简体   繁体   English

PHP str_replace

[英]php str_replace

How to str_replace 如何str_replace

</div> 
</li> 
</ul>

to

</div> 
</li> 
</ul>
aaa

All the tags in a new line. 所有标签都换行。

I tried 我试过了

$str = str_replace('</div>\n\r</li>\n\r</ul>', '</div>\n\r</li>\n\r</ul>\n\raaa', $str);  

UPDATE CODE: 更新代码:

$str =<<<EOT
</div>
</li>
</ul>
EOT;

$str = str_replace("</div>\n\r</li>\n\r</ul>", "</div>\n\r</li>\n\r</ul>\n\raaa", $str);  

echo $str;

\\n in single quotes is a literal \\ and a literal n , rather than a \\n , to get the line breaks you need to use double quotes: 单引号中的\\n是文字\\和文字n而不是\\n ,要得到换行符,您需要使用双引号:

$str = str_replace("</div>\r\n</li>\r\n</ul>", "</div>\r\n</li>\r\n</ul>\r\naaa", $str);  

Also, you should be replacing \\r\\n not \\n\\r as Windows line breaks are a carriage return \\r followed by a line break \\n . 另外,您应该替换\\r\\n而不是\\n\\r因为Windows换行符是回车符\\r后跟换行符\\n

When you use single quotes the \\r\\n will be treated as string. 当使用单引号时, \\r\\n将被视为字符串。

Use double quotes instead: 请使用双引号代替:

$str = str_replace("</div>\n\r</li>\n\r</ul>", "</div>\n\r</li>\n\r</ul>\n\raaa", $str); 

EDIT 编辑

Can't you just do: 你不能只是做:

$str = str_replace('</ul>', "</ul>\n\raaa", $str); 

If that isn't suited it's better to resort to rexeg. 如果不合适,最好使用rexeg。

First of all: Don't you just want this? 首先:您不只是想要这个吗?

$str = $str . "\n\raaa";

Second, (see Mark Elliot's answer about single versus double quotes) 第二,(请参阅Mark Elliot关于单引号与双引号的答案)

为什么不串连音?

$str .= "\r\naaa"; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM