我的代码中的未知修饰符 [英] Unknown modifier in my code
问题描述
<? php
$Src = 'images/pages/clients/logos/clnt_aljareera_img.jpg';
$pttn= '/&Src:'.$Src.'/';
$string=preg_replace($pttn,'',$string,1);
?>
//输出错误:
推荐答案
您的字符串包含一个完整的/
混乱,当使用/
作为正则表达式定界符时,需要将其作为\/
进行转义.代替/
作为正则表达式定界符,例如,使用在字符串中不会出现的内容,例如~
.您必须选择一个定界字符,但是保证 not 不会出现在$Src
中.即使使用|
,也可能比使用~
更安全.
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);
发生的事情是,用/
分隔的正则表达式在images/
之后立即遇到p
,因为它认为它已到达了结束定界符.下一个单词pages
被错误地视为正则表达式修饰符的字符串.
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看到正则表达式:
PHP sees the regular expression:
/&src:images/pages
这篇关于我的代码中的未知修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!