Tufan Tufan - 12 days ago 6
PHP Question

Regex add paragraph to new line

How can i convert this string


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pulvinar lorem lectus, vel aliquam est blandit accumsan. Maecenas nisi eros, ornare a euismod rutrum, rhoncus a massa.

Hello world

<b>Goodbye world</b>

<h2>Some headings</h2>


to html like this in php with regex?


<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pulvinar lorem lectus, vel aliquam est blandit accumsan. Maecenas nisi eros, ornare a euismod rutrum, rhoncus a massa.</p>

<p>Hello world</p>

<p><b>Goodbye world</b></p>

<h2>Some headings</h2>


Solution



You can find solution here. All answers were useful.

Answer

using preg_replace with multiline flag can achieve this.

See this:

$str = <<<"MIXED"
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus pulvinar lorem    lectus, vel aliquam est blandit accumsan. Maecenas nisi eros, ornare a euismod    rutrum, rhoncus a massa.
Hello world
<b>Goodbye world</b>
MIXED;

$str = preg_replace('/^(.*)$/m',"<p>$1</p>",$str);
echo $str;
Comments