How to replace htmlentities with HTML tags using PHP str_replace

I have content as below.

I want <pre></pre> be converted to <p> </p> . But I can’t achieve this. 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 conclusion as is. Can someone help me decide. thanks in advance

+5
source share
3 answers

You change $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 them match your str_replace

Delete htmlspecialchars() and you will get the desired result.

 $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 
+2
source

You can get the desired result without removing the htmlspecialchars () function by doing something like this:

 $content = "<pre>This is a pre text &#13; I want to convert it to paragraphs "; print_r(str_replace(array('&lt;pre&gt;', '&lt;/pre&gt;', '&amp;#13;'),array('<p>', '</p>', '<br/>'),htmlspecialchars($content))); 
0
source

Your code should be such as to get the expected result.

 <?php $content = "<pre>This is a pre text </pre> &#13; I want to convert it to paragraphs"; print_r(htmlspecialchars(str_replace(array('<pre>', '</pre>', '&#13;'),array('<p>', '</p>', '<br/>'),$content))); ?> 
0
source

Source: https://habr.com/ru/post/1269963/


All Articles