开发者

php str_replace

How to 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:

$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.


When you use single quotes the \r\n will be treated as string.

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.


First of all: Don't you just want this?

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

Second, (see Mark Elliot's answer about single versus double quotes)


Why do not string concat?

$str .= "\r\naaa"; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜