preg_match () [function.preg-match]: No ending delimiter '^'

0

I get some help in putting the correct delimiters below:

if (preg_match('/^[a-zA-Z0-9_-]+[/]{1}[a-zA-Z0-9_-]+$', $bloco)) {
  ....
}
    
asked by anonymous 20.03.2015 / 16:55

1 answer

3

You put the delimiter at the beginning of the expression, the same delimiter is missing at the end. Also, you did not escape the slash ( / ).

'/^[a-zA-Z0-9_-]+\/{1}[a-zA-Z0-9_-]+$/'

Another thing: When you require a single occurrence of a character, you do not need to use quantifiers. So, your expression would look like this:

'/^[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+$/'

Finally, one last improvement: character ranges can be replaced by character classes:

  • % by% by% by%
  • % by% by% by%

So, your expression would look like this:

'/^[\w\d_-]+\/[\w\d_-]+$/'
    
20.03.2015 / 17:21