开发者

Tag stripping allowing some html tags - Facebook-ish

开发者 https://www.devze.com 2023-03-14 01:41 出处:网络
I am doing something like posting function in a local app it\'s开发者_开发问答 working fine really but it lacks with validation and not to mention the validation I made was a mess. I\'m using jQuery o

I am doing something like posting function in a local app it's开发者_开发问答 working fine really but it lacks with validation and not to mention the validation I made was a mess. I'm using jQuery oEmbed.

What I wanted is to print the illegal html tag(s) as is and activate/perform(I don't know the right term) the html tags I have allowed.

Any suggestions?


This is the best solution i came up.

First replaced all the < and > for html code then replaced back the allowed tags.

<?php

$original_str = "<html><b>test</b><strong>teste</strong></html>";
$allowed_tags = array("b", "strong");

$sans_tags = str_replace(array("<", ">"), array("&lt;","&gt;"), $original_str);

$regex = sprintf("~&lt;(/)?(%s)&gt;~", implode("|",$allowed_tags));
$with_allowed = preg_replace($regex, "<\\1\\2>", $sans_tags);

echo $with_allowed;
echo "\n";

Result:

guax@trantor:~$ php teste.php 
&lt;html&gt;<b>test</b><strong>teste</strong>&lt;/html&gt

I wonder if there's any solution for replacing all at once. But it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消