Unknown modifier in my code [duplicate]

那年仲夏 提交于 2019-12-02 10:57:55

问题


<? php
    $Src = 'images/pages/clients/logos/clnt_aljareera_img.jpg';
    $pttn= '/&Src:'.$Src.'/';
    $string=preg_replace($pttn,'',$string,1);
?>

//output Error: Unknown modifier 'p' in


回答1:


Your string contains a whole mess of / which would need to be escaped as \/ when using / as the regex delimiter. Instead of / as the regex delimiters, use something which won't occur in your string like ~ for example. You must choose a delimiting character which is guaranteed not to appear in $Src, however. You might be safer even with | than with ~.

$Src = 'images/pages/clients/logos/clnt_aljareera_img.jpg';
// Delimit the regular expression with ~
$pttn= '~&Src:'.$Src.'~';
$string=preg_replace($pttn,'',$string,1);

What has happened is your regex delimited by / encounters a p immediately after images/ because it thinks it has reached the closing delimiter. The next word pages is mistakenly treated as a string of regex modifiers.

PHP sees the regular expression:

/&src:images/pages



回答2:


Remove the space in your opening php-tag.



来源:https://stackoverflow.com/questions/13017451/unknown-modifier-in-my-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!