Strip_tags and html_entity_decode are not working properly

I have been struggling with this problem since yesterday - unfortunately, to no avail (not completely, I found some workaround), and after some research and re-reading the documentation, I'm still a little stunned and confused.

Suppose there is an ugly line that already has special characters correctly encoded in html. eg:

$exampleString = '<div id="dynamic2"> <div
id="iStoreProductLongDescription" class="iStoreBox">
<div class="iStoreBoxWrapper"> <div 
class="iStoreBoxContent"> <p style="text-
align:justify;"> </p> <p style="text-
align:justify;"><span style="font-
size:large;"><span> Just a bunch of text here, at 
the end. yedyedyed';

Now I would like to output this string as a simple clean string, so first I will need to convert it to html tags, and then split the tags. Therefore, I assume that the following chain should work:

$result = strip_tags(htmlspecialchars_decode($exampleString));

but it is not, echo $ result output to the web page source:

<div id="dynamic2"> <div id="iStoreProductLongDescription" class="iStoreBox"> <div class="iStoreBoxWrapper"> <div class="iStoreBoxContent"> <p style="text-align:justify;"> </p> <p style="text-align:justify;"><span style="font-size:large;"><span> Just a bunch o text here, at the end. yedyedyed

In custom outputs, html tags are visible and remain intact. However, when I do something like this:

$result = strip_tags(html_entity_decode(htmlspecialchars_decode($exampleString)));

, -:

, . yedyedyed

-

strip_tags(htmlspecialchars_decode($exampleString));

( ), ? .

+4
2

, ,

< <.

, htmlspecialchars_decode

< <

, , , htmlspecialchars_decode ( html_entity_decode) <, strip_tags.

+3

:

$exampleString = '&amp;lt;div id="dynamic2"&amp;gt; &amp;lt;div
id="iStoreProductLongDescription" class="iStoreBox"&amp;gt;
&amp;lt;div class="iStoreBoxWrapper"&amp;gt; &amp;lt;div 
class="iStoreBoxContent"&amp;gt; &amp;lt;p style="text-
align:justify;"&amp;gt; &amp;lt;/p&amp;gt; &amp;lt;p style="text-
align:justify;"&amp;gt;&amp;lt;span style="font-
size:large;"&amp;gt;&amp;lt;span&amp;gt; Just a bunch of text here, at 
the end. yedyedyed';

$result = htmlspecialchars_decode($exampleString); // this will decode the entities
echo $result;

:

<div id="dynamic2"> <div
id="iStoreProductLongDescription" class="iStoreBox">
<div class="iStoreBoxWrapper"> <div 
class="iStoreBoxContent"> <p style="text-
align:justify;"> </p> <p style="text-
align:justify;"><span style="font-
size:large;"><span> Just a bunch of text here, at 
the end. yedyedyed

html_entity_decode():

$result = html_entity_decode($result); // this will print with HTML
echo $result;

( html):

 Just a bunch of text here, at 
the end. yedyedyed

strip_tags():

$result = strip_tags($result); // this will strip your html tags
$echo $result;

( html):

 Just a bunch of text here, at 
the end. yedyedyed
+2

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


All Articles