/**
* HTML转换为文本
*
* @param string $str 需要转换的字符串
* @param string $r 如果$r=0直接返回内容,否则需要使用反斜线引用字符串
* @return string
*/
if ( ! function_exists('Html2Text'))
{
function Html2Text($str,$r=0)
{
if($r==0)
{
return SpHtml2Text($str);
}
else
{
$str = SpHtml2Text(stripslashes($str));
return addslashes($str);
}
}
}function SpHtml2Text($str){$str = preg_replace("/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU","",$str);$alltext = "";$start = 1;for($i=0;$i<strlen($str);$i++){if($start==0 && $str[$i]==">"){$start = 1;}else if($start==1){if($str[$i]=="<"){$start = 0;$alltext .= " ";}else if(ord($str[$i])>31){$alltext .= $str[$i];}}}$alltext = str_replace(" "," ",$alltext);$alltext = preg_replace("/&([^;&]*)(;|&)/","",$alltext);$alltext = preg_replace("/[ ]+/s"," ",$alltext);return $alltext;}
/**
* 文本转HTML
*
* @param string $txt 需要转换的文本内容
* @return string
*/
if ( ! function_exists('Text2Html'))
{
function Text2Html($txt)
{
$txt = str_replace(" ", " ", $txt);
$txt = str_replace("<", "<", $txt);
$txt = str_replace(">", ">", $txt);
$txt = preg_replace("/[\r\n]{1,}/isU", "<br/>\r\n", $txt);
return $txt;
}
}
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛