preg_replace PHP - / or @ why put?

Asked

Viewed 393 times

3

Using preg_replace() in PHP, I came across the following situation:

echo preg_replace('/:([\w]+)/', 'batata', ':quiabo/isso/:nada');

echo preg_replace('@:([\w]+)@', 'batata', ':quiabo/isso/:nada');

Both expressions print the same thing: potato/it/potato

From that I have two doubts:

1 - Why we need to use '/' or '@' at the beginning of Pattern?

2 - What is the difference between using '/' or '@' in this same situation?

  • You can also use ~, I think it’s more readable depending on the ER.

  • Or # or %, or ~, etc..

1 answer

2

No difference, serves only to indicate the beginning (delimiters) and end of your regex

Update:

Only in case you want to marry a '@' then you would have to escape using the delimiters @

ex.

@\@@

or

/@/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.