简体   繁体   中英

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)

为什么不串连音?

$str .= "\r\naaa"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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