将 javascript 替换转换为 php preg_replace 或 str_replace

发布于 2024-11-04 01:30:35 字数 710 浏览 6 评论 0原文

我有两个可以正常工作的 javascript 替换命令。我需要让它们在 PHP 中工作。

var body = body.replace(/\n\n<blockquote>/g, '<blockquote>');
var body = body.replace(/<\/blockquote>\n\n/g, '<\/blockquote>');

我尝试了很多很多不同的正则表达式可能性和很多 str_replace 但它们都失败了。我认为可行的如下,但它们不起作用:

$body = preg_replace('/\n+<blockquote>/gi', '<blockquote>', $body);
$body = preg_replace('/</blockquote>\\n+/gi', '</blockquote>', $body);

$body = str_replace( "\n\n<blockquote>", "<blockquote>", $body);
$body = str_replace( "<blockquote>\n\n", "</blockquote>", $body);

基本上,我只需要删除块引用标签周围的两个换行符。是的,我已经确认它们是新行而不是回车符。

提前致谢!

I have the two javascript replace commands which work pefectly. I need to make them work in PHP.

var body = body.replace(/\n\n<blockquote>/g, '<blockquote>');
var body = body.replace(/<\/blockquote>\n\n/g, '<\/blockquote>');

I have tried many, many different regex possibilites and many str_replace's but all of them fail. What I thought would work is below, but they do not:

$body = preg_replace('/\n+<blockquote>/gi', '<blockquote>', $body);
$body = preg_replace('/</blockquote>\\n+/gi', '</blockquote>', $body);

$body = str_replace( "\n\n<blockquote>", "<blockquote>", $body);
$body = str_replace( "<blockquote>\n\n", "</blockquote>", $body);

Basically, I just need to remove the two newlines which surround the blockquote tags. Yes, I have confirmed they are new lines and not carriage returns.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

ま昔日黯然 2024-11-11 01:30:35

您在上次调用中错过了 /

$body = str_replace("</blockquote>\n\n", "</blockquote>", $body);

通过此修复,您的 str_replace 解决方案应该可以工作。我注意到您在正则表达式中使用了 i 修饰符。也许您想使用 str_ireplace 进行不区分大小写的替换。

You have missed a / in the last call:

$body = str_replace("</blockquote>\n\n", "</blockquote>", $body);

With this fix, your str_replace solution should work. I notice that you use the i modifier in your regular expressions. Maybe you want to use str_ireplace to make an case-insensitive replacement.

七堇年 2024-11-11 01:30:35

我认为代码是正确的,问题可能是标记之后或之前存在除换行符之外的字符。 \n 可能在那里,但我很确定那里也可能有一些空格。

出于调试目的尝试执行此操作

  • 替换新行

    $body = str_replace("\n", '--newline--', $body);

  • 替换空格。

    $body = str_replace(" ", '--whitespace--', $body);

  • 与身体相呼应

    echo $body;

如果你那里有什么东西,你现在就会注意到它。
祝你好运 :)

I think the code is correct and the problem might be the presence of characters other then the newlines after or before your tag. The \n might be there but i am pretty sure you might have some spaces in there too.

for debugging purposes trying doing this

  • Replace your new lines

    $body = str_replace("\n", '--newline--', $body);

  • replace your white spaces.

    $body = str_replace(" ", '--whitespace--', $body);

  • echo the body

    echo $body;

if you have anything there, you will notice it now.
Good luck :)

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