Search code examples
phppreg-matchereg

Error updating ereg call to use preg_match instead


I'm trying to convert the following ereg() call to use preg_match() instead:

if (ereg('(.*/)(.*)$',$fileref,$reg)    )   {

This is the preg_match() call I'm attempting to replace it with:

if (preg_match('(.*/)(.*)$',$fileref,$reg)  )   {

When I run this code, I am presented with the following error:

preg_match(): Unknown modifier '('

What is the difference in syntax between ereg() and preg_match() that is causing this error?


Solution

  • preg_match('~(.*/)(.*)$~',$fileref,$reg)
    

    You must define delimiters. I used ~, but you can use any delimiter you like (its always the first character). The pattern itself must end with the delimiter (but modifiers can follow) and the delimiter must be escaped, when used within the pattern.

    But worth to mention: The pattern is only looking for a /, thus regular expression is just oversized.

    if (strpos($fileref, '/') !== false) { /* do something */ }