将 javascript 替换转换为 php preg_replace 或 str_replace
我有两个可以正常工作的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在上次调用中错过了
/
:通过此修复,您的 str_replace 解决方案应该可以工作。我注意到您在正则表达式中使用了
i
修饰符。也许您想使用 str_ireplace 进行不区分大小写的替换。You have missed a
/
in the last call: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.我认为代码是正确的,问题可能是标记之后或之前存在除换行符之外的字符。
\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 :)