PHP 中的 preg_replace 使用什么分隔符(替换在 PHP 外部工作但不在 PHP 内部工作)
我自己和我的团队都陷入了这个困境,我有以下代码。
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut bibendum augue eu arcu mollis cursus. Curabitur nec purus ipsum. Fusce ut erat leo, vitae malesuada lacus. Quisque varius gravida justo ut aliquam. Integer accumsan, ante non volutpat semper, orci sem luctus odio, sit amet convallis odio justo id nisl. Nunc sed lacus nisi, quis accumsan massa. Donec ante enim, fermentum sit amet placerat nec, eleifend elementum nibh
[[BLOGIMAGE_20090303011757.jpg||480]]
us dolor nec est. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam accumsan blandit purus eget vestibulum. Nullam neque sem, suscipit sit amet mattis eu, imperdiet quis ligula. Integer aliquam dapibus gravida. Pellentesque ultrices sapien orci. Suspendisse at eros non dolor accumsan cursus mattis nec justo.
[[BLOGIMAGE_20090303011819.jpg||480]]
Aenean cursus lacinia arcu vitae malesuada. Fusce fermentum enim sit amet elit fermentum at consectetur ante vulputate. Aliquam sagittis nulla id magna facilisis tempus. Suspendisse eget feugiat libero. Pellentesque non lorem sem, eu posuere velit. Nulla id nulla ligula.
[[BLOGIMAGE_20090303011842.jpg||480]] ..... etc";
$pat = "\[\[(.*)\|\|(.*)\]\]";
$mat = '<img src="/path/to/file/imgs/$1" width="$2px" />';
$text = preg_replace($pat , $mat, $text);
我们想要做的是使用 $mat 中的结构将 [[imagefile||size]] 转换为图像标签。该匹配在 RegExr(一个 adobe air 程序)和各种 javascript 在线测试程序中完美运行
我得到的错误是:-
消息:preg_replace() [function.preg-replace]:分隔符 不能是字母数字或反斜杠
所以如果我添加一个分隔符,例如......
$pat = "^[[(.)\|\|(.)]]^";
然后匹配停止工作,但我不再收到任何错误。
任何帮助真的将不胜感激。
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 arcu mollis cursus. Curabitur nec purus ipsum. Fusce ut erat leo, vitae malesuada lacus. Quisque varius gravida justo ut aliquam. Integer accumsan, ante non volutpat semper, orci sem luctus odio, sit amet convallis odio justo id nisl. Nunc sed lacus nisi, quis accumsan massa. Donec ante enim, fermentum sit amet placerat nec, eleifend elementum nibh
[[BLOGIMAGE_20090303011757.jpg||480]]
us dolor nec est. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Etiam accumsan blandit purus eget vestibulum. Nullam neque sem, suscipit sit amet mattis eu, imperdiet quis ligula. Integer aliquam dapibus gravida. Pellentesque ultrices sapien orci. Suspendisse at eros non dolor accumsan cursus mattis nec justo.
[[BLOGIMAGE_20090303011819.jpg||480]]
Aenean cursus lacinia arcu vitae malesuada. Fusce fermentum enim sit amet elit fermentum at consectetur ante vulputate. Aliquam sagittis nulla id magna facilisis tempus. Suspendisse eget feugiat libero. Pellentesque non lorem sem, eu posuere velit. Nulla id nulla ligula.
[[BLOGIMAGE_20090303011842.jpg||480]] ..... etc";
$pat = "\[\[(.*)\|\|(.*)\]\]";
$mat = '<img src="/path/to/file/imgs/$1" width="$2px" />';
$text = preg_replace($pat , $mat, $text);
What we want to do is convert the [[imagefile||size]] into the image tag using the structure in the $mat. The match works perfectly in RegExr (an adobe air program), and verious javascript online testers
The error I'm getting is :-
Message: preg_replace()
[function.preg-replace]: Delimiter
must not be alphanumeric or backslash
So if I add a dilimiter, such as...
$pat = "^[[(.)\|\|(.)]]^";
Then the match up stops working but I no longer get any errors.
Any help really would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
preg_replace()
需要一个分隔符:传统上它是正斜杠,但它可以是任何字符 - 特别是当您在正则表达式本身中需要正斜杠时,您可以诉诸另一个字符。
这种灵活性允许您将
"/http:\/\/foo\/bar\//"
(“倾斜牙签综合症”)表示为"!http://foo/bar/ !”
。分隔符是将正则表达式与正则表达式标志(也称为“修饰符”)分开所必需的,例如:
…这使用
i
标志来声明不区分大小写的正则表达式。preg_replace()
requires a delimiter character: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:
…this uses the
i
flag to declare a case-insensitive regex.来自 PCRE 分隔符的 PHP 手册:
因此,您可以使用
/
作为分隔符将模式与可选的 修饰符:但还要注意:
此外,目前您的模式将尽可能匹配,因为两个 量词 都是贪婪的;您可能想要将它们更改为不愿意只匹配尽可能少的内容:
From the PHP manual on PCRE delimiters:
So you could use
/
as delimiter to separate the pattern from optional modifiers:But also note:
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:
尝试
try