What Delimiter to use for preg_replace in PHP (replace working outside of PHP but not inside)

后端 未结 3 689
天涯浪人
天涯浪人 2020-11-27 07:41

Myself and my team are stuck on this one, I have the following code.

$text = \"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut bibendum augue eu         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 08:07

    preg_replace() requires a delimiter character:

    preg_replace("/$pat/" ...
    

    Traditionally it's the forward slash, but it can be any character - especially when you need the forward slash in the regex itself you can resort to another character.

    This flexibility allows you to express "/http:\/\/foo\/bar\//" ("leaning toothpick syndrome") as "!http://foo/bar/!".

    The delimiter character is necessary to separate the regex from the regex flags (a.k.a. "modifiers"), for example:

    preg_replace("/$pat/i" ...
    

    …this uses the i flag to declare a case-insensitive regex.

提交回复
热议问题