Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace htmlentities using HTML Tags using PHP str_replace

I have a content like below.

I want the <pre></pre> to be converted to <p> </p>. But i'm not able achieve it. Below is an example

$content = "<pre>This is a pre text &#13; I want to convert it to paragraphs ";

print_r(str_replace(array('<pre>', '</pre>', '&#13;'),array('<p>', '</p>', '<br/>'),htmlspecialchars($content)));

But i get the output as it is. Can someone help me resolve. Thanks in advance

like image 623
Jeeva Avatar asked Nov 07 '22 19:11

Jeeva


1 Answers

You are changing $content before replacing the string.

$content = "<pre>This is a pre text &#13; I want to convert it to paragraphs ";

print_r(htmlspecialchars($content));

// returns    &lt;pre&gt;This is a pre text &amp;#13; I want to convert it to paragraphs 

None of which matches your str_replace

Remove the htmlspecialchars() and you will get the output you wanted.

$content = "<pre>This is a pre text &#13; I want to convert it to paragraphs ";

print_r(str_replace(array('<pre>', '</pre>', '&#13;'),array('<p>', '</p>', '<br/>'),$content));

//returns    <p>This is a pre text <br/> I want to convert it to paragraphs 
like image 85
Luke Avatar answered Nov 15 '22 06:11

Luke