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

后端 未结 3 700
天涯浪人
天涯浪人 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:30

    From the PHP manual on PCRE delimiters:

    A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.

    Often used delimiters are forward slashes (/), hash signs (#) and tildes (~).

    So you could use / as delimiter to separate the pattern from optional modifiers:

    /\[\[(.*)\|\|(.*)\]\]/
    

    But also note:

    In addition to the aforementioned delimiters, it is also possible to use bracket style delimiters where the opening and closing brackets are the starting and ending delimiter, respectively.

    Furthermore, currently your pattern will match as much as possible as both quantifiers are greedy; you might want to change them to be reluctant to only match as little as possible:

    /\[\[(.*?)\|\|(.*?)\]\]/
    

提交回复
热议问题