Jump to content

URL Validation JavaScript Regex to PHP Regex


OpenCode

Recommended Posts

Hello,

 

I got a JavaScript URL Validation Regex, since I'm not used-to with Regex I need help converting it  into PHP regex.

 

Here is a JavaScript Regex

/^https?\:\/\/(www\d?\d?\d?\d?\.)?([A-Za-z0-9-_]+\.)?[A-Za-z0-9-_]+((\.[A-Za-z]{2,6})(\.[A-Za-z]{2})?([0-9-_%&\?\/\.=]*))$/

 

I need this particular regex in PHP

 

 

Thanks in advance

the regex is the same (the syntax is the same, though I can't vouch for the soundness of your pattern without knowing details...but if you know your current pattern works for you, then okay), but you would use the pattern in something like preg_match

 

example:

 

preg_match("/.../",$contentToMatch,$matches);
echo "<pre>";
print_r($matches);

 

first argument is the pattern, 2nd is the subject, 3rd are results of captured matches.  Or if you are not caring about the captured groups and just wanting to know if the pattern matched overall:

 

if (preg_match("/.../",$contentToMatch)) {
  // matched
} else {
  // did not match
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.