php - URL validation using eregi() but now with preg_match -


eregi() deprecated , need replace preg_match. how can convert following syntax preg_match? suppose following url , it's required regex pattern:

$url = "https://james.wordpress.com/2013/05/13/my-product-final"; $urlregex = "^(https?|ftp)\:\/\/([a-zåäöÅÄÖæÆøØ0-9+!*(),;?&=\$_.-]+(\:[a-zåäöÅÄÖæÆøØ0-9+!*(),;?&=\$_.-]+)?@)?[a-zåäöÅÄÖæÆøØ0-9+\$_-]+(\.[a-zåäöÅÄÖæÆøØ0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-zåäöÅÄÖæÆøØ0-9+\$_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-zåäöÅÄÖæÆøØ0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-zåäöÅÄÖæÆøØ0-9+\$_.-]*)?\$"; 

following code works fine:

if (eregi($urlregex, $url)) { echo "valid"; exit; } 

i tried convert 'preg_match' couldn't work:

if (preg_match("#$urlregex#i", $url)) { echo "valid"; exit; } 

also tried this:

if (preg_match('/'.$urlregex.'/i', $url)) 

it doesn't work because char you're using delimiter (ie. #) present in regex.

you can:

  • escape # in regex: \#
  • change delimiter char not present in regex

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -