PHP preg_replace pattern only seems to work if its wrong?

前端 未结 1 702
陌清茗
陌清茗 2021-01-22 13:26

I have a string that looks like this

../Clean_Smarty_Projekt/tpl/templates_c\\.
../Clean_Smarty_Projekt/tpl/templates_c\\..

I want to replace <

1条回答
  •  悲&欢浪女
    2021-01-22 14:02

    I suggest you to use a different php delimiter. Within the / delimiter, you need to use three \\\ or four \\\\ backslashes to match a single backslash.

    $string = '../Clean_Smarty_Projekt/tpl/templates_c\.'."\n".'../Clean_Smarty_Projekt/tpl/templates_c\..';
    echo preg_replace('~\.\./|\\\.{1,2}~', '', $string)
    

    Output:

    Clean_Smarty_Projekt/tpl/templates_c
    Clean_Smarty_Projekt/tpl/templates_c
    

    0 讨论(0)
提交回复
热议问题