", "", "". The rest of the characters that I would lik...">

Htmlentities with exceptions

I have several possible tags, for example "<main>", "<text>", "<tag>". The rest of the characters that I would like to handle using htmlentities (htmlspecialchars)

<main>
<text>
<tag> <>  X&Y <  <falsetag> <tag attr="123" /> </tag>
</text>
</main>

The result should be

<main>
<text>
<tag> &lt;&gt;  X&amp;Y &lt;  &lt;falsetag&gt; <tag attr="123" /> </tag>
</text>
</main>

What is the best way to do this.

+3
source share
3 answers

You can run htmlentities in text and then use regex to replace valid tags <>

Example...

$str = '<main>
<text>
<tag> <>  X&Y <  <falsetag> <tag attr="123" /> </tag>
</text>
</main>
';

$allowed_tags = array( 'tag', 'text', 'main' );

$escaped_str = htmlentities( $str );

$replace_what = array_map( function($v){ return "~&lt;(/?)$v(.*?)&gt;~"; }, $allowed_tags );
$replace_with = array_map( function($v){ return "<$1$v$2>"; }, $allowed_tags );

echo preg_replace( $replace_what, $replace_with, $escaped_str );
+2
source

The only solution I see is to load it into an XML parser, and then recursively build the output string yourself, but this will take a bit of work.

. (, ) , >.

+1

I have a simple solution that worked well for me:

$text = htmlentities($text, ENT_QUOTES, "UTF-8");
$text = htmlspecialchars_decode($text);
$text = strip_tags($text, "<p><b><h2>");
+1
source

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


All Articles