James Dale
James Dale

Reputation: 126

Combining htmlspecialchars and strip_tags

So I'm wanting to combine the functionality of htmlspecialchars and strip_tags.

The elements I have so far are:

<form>
....
<section contenteditable="true" placeholder="Please enter your text content here..." class="content" id="content" name="content" type="text" cols="30" rows="10"></section>
....
</form>

This is then submitted

....
$content = $_POST['content'];

...Some SQL...

$content = $c['content'];
$new = htmlspecialchars($content, ENT_QUOTES);

I wanting to combine htmlspecialchars and strip_tags together, they both work separately and produce the right result.

$new = strip_tags($content, '<code><b><i>');
....

So and example would be that this tag:

<section>

would be displayed as plain text where as this tag:

<code>

would be formatted correctly.

Upvotes: 0

Views: 499

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

Something like this should work, maybe put it in a function:

$allowed = strip_tags($content, '<code><b><i>');

$content = htmlspecialchars($content, ENT_QUOTES);

$content = str_replace(htmlspecialchars($allowed, ENT_QUOTES), $allowed, $content);

Upvotes: 1

Related Questions