PHP 中的 preg_replace 使用什么分隔符(替换在 PHP 外部工作但不在 PHP 内部工作)

发布于 2024-10-04 23:56:34 字数 1798 浏览 2 评论 0原文

我自己和我的团队都陷入了这个困境,我有以下代码。

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冬天的雪花 2024-10-11 23:56:34

preg_replace() 需要一个分隔符:

preg_replace("/$pat/" ...

传统上它是正斜杠,但它可以是任何字符 - 特别是当您在正则表达式本身中需要正斜杠时,您可以诉诸另一个字符。

这种灵活性允许您将 "/http:\/\/foo\/bar\//" (“倾斜牙签综合症”)表示为 "!http://foo/bar/ !”

分隔符是将正则表达式与正则表达式标志(也称为“修饰符”)分开所必需的,例如:

preg_replace("/$pat/i" ...

…这使用 i 标志来声明不区分大小写的正则表达式。

preg_replace() requires a delimiter character:

preg_replace("/$pat/" ...

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:

preg_replace("/$pat/i" ...

…this uses the i flag to declare a case-insensitive regex.

我不咬妳我踢妳 2024-10-11 23:56:34

来自 PCRE 分隔符的 PHP 手册

分隔符可以是任何非字母数字、非反斜杠、非空格字符。

常用的分隔符是正斜杠 (/)、井号 (#) 和波形符 (~)。

因此,您可以使用 / 作为分隔符将模式与可选的 修饰符:

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

但还要注意:

除了上述分隔符之外,还可以使用括号样式分隔符,其中左括号和右括号分别是开始和结束分隔符。

此外,目前您的模式将尽可能匹配,因为两个 量词 都是贪婪的;您可能想要将它们更改为不愿意只匹配尽可能少的内容:

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

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:

/\[\[(.*?)\|\|(.*?)\]\]/
护你周全 2024-10-11 23:56:34

尝试

$pat  = "/\[\[(.*)\|\|(.*)\]\]/m";

try

$pat  = "/\[\[(.*)\|\|(.*)\]\]/m";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文