Regex encapsulation php

What is the difference / disadvantages of using "/" or "#" as regex encapsulation

eg.

'/' = preg_match('/MYSEARCH}(.+)ENDMYSEARCH/s',$out,$matches);

'#' = preg_match('#MYSEARCH}(.+)ENDMYSEARCH#s',$out,$matches);

      

Thank!

+2


source to share


2 answers


The only thing that matters for PHP / PCRE is that you must use the same character at the beginning and at the end .

It's important for you to avoid: inside a regex, you need to escape the delimiter if you want to use it.

For example, to match a portion of a URL using / as a delimiter, a regex might look like this:

'/^http:\/\/www/'

      

When using /

as a separator, you need to escape the forward slashes inside the regex - not easy and doesn't look good.



On the other hand, using #

as a separator:

'#http://www#'

      

Much easier to write and read, isn't it?


Depending on your needs and / or preference, you can use any character you want - sometimes I see " !

" or " ~

", for example.

+7


source


Also remember that with preg_quote you can add a separator for escaping.



+1


source







All Articles