Home > other >  Decoding too many times encoded string in PHP
Decoding too many times encoded string in PHP

Time:01-15

I get a string in PHP from an external database which looks like this:

$myStr = '<br>To send a note'

As you can see this string is encoded too many times, is there is a way to decode it all the way back using PHP?

CodePudding user response:

What happened that your str passed lots of times through htmlentities().

The original string probably was <br>To send a note, then, the 1st time it become &lt;br&gt;To send a note, the secong it replace all & with &amp; and so on.

In order to put it inside text area you should to decode it using:

<textarea><?php echo html_entity_decode($myStr);  ?></textarea>

The code bellow will pass as many times it's necessary to solve your issue:

    $modStr = $myStr = '&amp;amp;amp;amp;amp;amp;amp;lt;br&amp;amp;amp;amp;amp;amp;amp;gt;To send a note';
do {
   $myStr = $modStr;
   $modStr = html_entity_decode($myStr);
} while( $modStr != $myStr );

[]s Andrei

  •  Tags:  
  • Related