Forum Moderators: coopster

Message Too Old, No Replies

check and clean data before insert into db

web address check and clean before insert into db

         

nofia

7:48 pm on Aug 7, 2008 (gmt 0)

10+ Year Member



i have a few ways i check and clean some basic data before i enter it into my database.

can anyone suggest code to check and clean a web address before I insert into db, i have a form where i ask for contact info details and one question is to enter your web address, i just want to check what is entered before i post it to db.

thanks
jd

eelixduppy

1:42 am on Aug 8, 2008 (gmt 0)



you want to check it or you want to escape it? If the latter, mysql_real_escape_string [php.net] should be good enough for what you want. If you want to check to see if it's really a URL properly formatted then you can use regular expressions for something like that.

paulmadillo

3:46 pm on Aug 8, 2008 (gmt 0)

10+ Year Member



Try this regex:

^((https?¦ftp)\://((\[?(\d{1,3}\.){3}\d{1,3}\]?)¦(([-a-zA-Z0-9]+\.)+[a-zA-Z]{2,4}))(\:\d+)?(/[-a-zA-Z0-9._?,'+&%$#=~\\]+)*/?)$

It's a tricky one to deal with and I haven't found any that can check every possible url but this one checks the format pretty well.

ag_47

8:53 pm on Aug 8, 2008 (gmt 0)

10+ Year Member



As of PHP5 you got a whole bunch of filer functions. Simple one:

bool filter_var('example.com', FILTER_VALIDATE_URL);

Although I think these functions are based on regex matching anyway, so you can go ahead and do the matching yourself instead.

nofia

4:32 pm on Aug 11, 2008 (gmt 0)

10+ Year Member



thanks for the replys. there are so many possibilities for someone to enter into a form when asking for a web address thats it is tricky to check the input.