我有這樣的輸出:<a href="#">sdfsdfsd</a>gdsfgsdfgsdgsdggsdfg
PHP註釋://或URL和html標籤的其餘部分是純文本
問題是,這個函數的所有HTML標籤轉換爲純文本除了那些URL像www.facebook.com (將其轉換爲<a href="www.facebook.com">www.facebook.com</a>
):
function validate_text($text = '') {
// This method is used internally as a FILTER_CALLBACK
if (mb_strlen($text, 'utf8') < 1)
return false;
// Encode all html special characters (<, >, ", & .. etc) and convert
//$str = nl2br(htmlspecialchars($text));
$str = htmlspecialchars($text);
// the new line characters to <br> tags:
// Remove the new line characters that are left
$str = str_replace(array(chr(10), chr(13)), '', $str);
$text = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $str);
$ret = ' ' . $text;
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,[email protected]\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,[email protected]\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\[email protected]\\3\">\\[email protected]\\3</a>", $ret);
//$ret = preg_replace("#^*@([)([0-9-])(])#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
$ret = substr($ret, 1);
return $ret;
}
我想是從<a href="#">something</a> <small>hello</small>
到<a href="#">something</a>sadfsafasf <small> hello </small>
沒有推出自己的HTML驗證,你這樣做是錯誤的。 – zzzzBov 2012-04-06 14:05:04
對不起,我不擅長RegEx我剛剛找到代碼....我該怎麼做配偶? – 2012-04-06 14:07:09
[對於初學者,請勿使用RegEx來解析HTML](http://stackoverflow.com/a/1732454/497418)。 – zzzzBov 2012-04-06 14:08:44