PHP Replace With New Line

2019-09-19 20:06发布

On a html page I have this.

</h2>
<ol>

The page is contained in a variable called $content. I want to replace this with something else but the code doesn't work.

$content = str_replace("</h2><ol>", $title, $content);

What should i put there instead of .

标签: php wordpress
3条回答
Lonely孤独者°
2楼-- · 2019-09-19 20:43

Depending on your IDE / text editor, or how you're accessing your file (ie: ssh/ftp/etc.) You may also consider using or playing around with one of the following escape characters for newline

"</h2>\r\n<ol>" or "</h2>\n<ol>" or even "</h2>\r<ol>"
查看更多
做自己的国王
3楼-- · 2019-09-19 20:45

Rather use preg_replace for this:

preg_replace("/<\/h2>[\s]*<ol>/", $title, $content);

Regardless of how you do it, it doesn't really make sense. An <h2> is started, but you remove that closing tag and the starting tag for the <ol>, which will also have a closing tag I assume? Seems like your output is going to be wrong regardless.

查看更多
狗以群分
4楼-- · 2019-09-19 20:46

\n is the new line character:

$content = str_replace("</h2>\n<ol>", $title, $content);

Just make sure you always use it in double quotes for PHP to recognize it as an escape sequence otherwise it will interpret it as a literal "slash" "n".

查看更多
登录 后发表回答