6

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

2
  • <pre>This give space between <pre> and This and try Commented Jul 19, 2017 at 6:53
  • htmlspecialchars is the culprit. You are changing the $content before replacing it. Commented Jul 19, 2017 at 6:56

3 Answers 3

3

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 
Sign up to request clarification or add additional context in comments.

7 Comments

so i should remove htmlspecialchars is what your saying
Unless the words "Remove the htmlspecialchars()" means something different?
Or just swap the order and strreplace first followed by htmlspecialchars.
@doug OPs question said he wanted the result to be <p> </p>. This achieves that. htmlspecialchars() gives him &lt;pre&gt; &lt;/pre&gt; instead.
I assumed he also needed the string converted for what ever reason. Hence why he was using htmlspecialchars in the first place.
|
0

your code should be like this to getting expected output.

<?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)));
?>

1 Comment

check it ,may be its to be match your desired output @jeeva
-1

You can get your desired output 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)));

2 Comments

That would give him &lt;pre&gt; &lt;/pre&gt;. In his question he said he wanted <pre> </pre>.
He said that he wants the <pre></pre> to be converted to <p></p>. And my code do that. I have just showed a way to get his result without removing the htmlspecialchars() function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.