删除str_replace中的换行符

时间:2014-05-27 23:11:10

标签: php str-replace

我正在尝试使用str_replace删除包含以下内容的模板中出现的任何html:

 <br><br style='font-size:10px;'>
 <br><br style='font-size:10px;'>

但对于我的生活,即使使用\ n

,我也无法“检测”换行符
 $replacestring = "<br><br style='font-size:10px;'>\n<br><br style='font-size:10px;'>";

 $output = str_replace($replacestring, $empty, $output);

如果我只做一行,它会检测到它,例如:

 $replacestring = "<br><br style='font-size:10px;'>";

为什么没有检测到换行? $ output是FINAL html输出

3 个答案:

答案 0 :(得分:1)

str_replace()命令的语法显示输出是第二个参数,而不是最后一个。

以下示例可以帮助您:

<?php
 $template_string = "<br><br style='font-size:10px;'>line1\n<br><br style='font-size:10px;'>line2";
 $replace_string = "<br><br style='font-size:10px;'>";
 $output = str_replace($replace_string, $output, $template_string);
 echo $output;
?>

然后输出显示:

line1
line2

希望这会有所帮助..

答案 1 :(得分:0)

如果您不关心性能,我会尝试使用preg_replace,这样您就可以使用正则表达式更轻松地捕获具有多个换行符的字符串。

试试这个:

$replacestring="<br><br style='font-size:10px;'>line1\n<br><br style='font-size:10px;'>line2";

$pattern = '/([\\n]+)?\<br([^>]+)?\>/';

echo preg_replace($pattern,'', $replacestring);

答案 2 :(得分:0)

原来另一个插件正在执行AFTER,所以脚本永远无法正常替换。感谢帮助人员