mowgli
mowgli

Reputation: 2869

preg_match, find and extract

I'm using this to determine if a string looks like the right url, and then extract a value

if (preg_match('#^(https?:\/\/)?(www.)?site.com\/cat\/(aaa\d{5,30})\/#', $url, $urlid)) {

   echo $urlid[3];

}

It works ok, but I think it could be more lean/improved (or just more good-practice). Should I add some "negative look-ahead", "non-capturing" or other?

This for example (https?:\/\/)?(www.)? was just a quick thought. It works, but... Should it really be in parentheses or is there a better way?

Upvotes: 1

Views: 48

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

You can write it like this:

if (preg_match('~^(?:https?://)?(?:www\.)?site\.com/cat/(aaa\d{5,30})/~', $url, $urlid))
    echo $urlid[1];

Improvements:

  • no need to use capturing groups when you don't need to capture something, use a non-capturing group instead (?:...)
  • no need to escape / when it is not used as delimiter
  • you need to escape . otherwise it is not seen as a literal character

Benefits:

  • the pattern is no more ambigous since \. matches a dot and not an other character
  • it's a little more readable since there is no more useless backslashes
  • a small memory/speed gain with the use of non-capturing groups

Upvotes: 1

Related Questions