mattspriggs28 Posted February 7, 2011 Share Posted February 7, 2011 Hi, I have the following regular expression check to check for a valid URL: preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $_prodLink) This works great, except when a URL contains rounded brackets. Does anyone know how I can amend the above to allow the rounded brackets ( ) in the URL? Quote Link to comment Share on other sites More sharing options...
zenlord Posted February 7, 2011 Share Posted February 7, 2011 http://php.net/manual/en/book.filter.php Whatever you make of it yourself: it will not be as good as PHP's own filters... Quote Link to comment Share on other sites More sharing options...
atrum Posted February 7, 2011 Share Posted February 7, 2011 While those filters php provides are useful at times, I highly recommend you read up on regular expressions. Being able to customize your filter makes a world of difference. preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $_prodLink) Near as I can tell you need to have your delimiters set as forward slashes ( / ) not pipes ( | ) Change to this preg_match('/^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$/i', $_prodLink) Here is a good site to get started. http://www.skdevelopment.com/php-regular-expressions.php Here is a few that I use all the time. function valPassword($input, $description, $strength = 1){ switch($strength){ case 1: //Low: 6 - 20 Characters Case insensitive. $pattern = "/^(?=.*[a-zA-Z0-9]).{6,20}$/"; break; case 2: //Medium: 6 - 20 Characters: Case Insensitive : 1 Number Required. $pattern = "/^(?=.*\d)(?=.*[a-zA-Z]).{6,20}$/"; break; case 3: //High: 8 - 20 Characters: Case Sensitive, 1 upper case, 1 lower case, 1 number Required $pattern = "/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/"; break; case 4: //Max: Same as High, but requires 1 Special Character $pattern = "/^(?=.*\!\@\#\$\-\_)(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,20}$/"; break; } $result = preg_match($pattern,$input); if($result){ return true; }else{ $this->errors[] = $description; return false; } } function valEmail($input, $description){ //Validates correct email syntax. $result = preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",$input); if($result){ return true; }else{ $this->errors[] = $description; return false; } } Quote Link to comment Share on other sites More sharing options...
atrum Posted February 7, 2011 Share Posted February 7, 2011 Oh, sorry I forgot to answer your question. I get so passionate about regex haha. Check this guide out. The answer you seek is near the bottom, it should at the very least give you a good idea. http://www.skdevelopment.com/php-regular-expressions.php Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.