編碼-截取中文-去除HTML字元

來源:互聯網
上載者:User

標籤:back   http   index   get   中文字元   sub   href   pad   tar   

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><?phpheader(‘Content-Type: text/html; charset=utf-8‘);function content_replace($show_content){     $search = array(        "‘<script[^>]*?>.*?</script>‘si", //過濾SCRIPT標記        "‘<iframe[^>]*?>.*?</iframe>‘si", //過濾IFRAME標記        "‘<style[^>]*?>.*?</style>‘si", //過濾STYLE標記        "‘<[\/\!]*span[^<>]*?>‘si", //過濾SPAN標記        "‘<[\/\!]*strong[^<>]*?>‘si",         "‘<[\/\!]*span[^<]*?>‘si", //過濾SPAN標記        "‘<[\/\!]*br[^<>]*?>‘si", //過濾br標記        "‘<[\/\!]*img[^<>]*?>‘si", //過濾img標記        "‘<[\/\!]*input[^<>]*?>‘si", //過濾input標記        "‘<[\/\!]*font[^<>]*?>‘si", //過濾FONT標記        "‘<[\/\!]*textarea[^<>]*?>‘si", //過濾textarea標記        "‘<[\/\!]*form[^<>]*?>‘si", //過濾form標記        "‘<[\/\!]*a[^<>]*?>‘si", //過濾A標記,如果要過濾連結中的字,必須在過濾規則中寫下。        "‘<[\/\!]*div[^<>]*?>‘si", //過濾DIV標記        "‘<[\/\!]*tbody[^<>]*?>‘si", //過濾TABLE標記        "‘<[\/\!]*table[^<>]*?>‘si", //過濾TABLE標記        "‘<[\/\!]*tr[^<>]*?>‘si", //過濾TABLE標記        "‘<[\/\!]*td[^<>]*?>‘si", //過濾TABLE標記        "‘<[\/\!]*p[^<>]*?>‘si", //過濾P標記        "‘<[\/\!]*o:p[^<>]*?>‘si", //過濾o:p標記        "‘<[\?\!]*xml:[^<>]*?>‘si", //過濾xml:標記        "‘<[\/\!]*select[^<>]*?>‘si", //過濾select標記        "‘<[\/\!]*option[^<>]*?>‘si", //過濾option標記        "‘<[\/\!]*v:[^<>]*?>‘si", //過濾v:標記        "‘<[\/\!]*o:[^<>]*?>‘si", //過濾o:標記        "‘target=([^\s>]*)|method=([^\s>]*)|action=([^\s>]*)|rows=([^\s>]*)|cols=([^\s>]*)|type=([^\s>]*)|href=([^\s>]*)|src=([^\s>]*)|id=([^\s>]*)|class=([^\s>]*)|name=([^\s>]*)|align=([^\s>]*)|cellpadding=([^\s>]*)|cellspacing=([^\s>]*)|border=([^\s>]*)|bordercolor=([^\s>]*)|background=([^\s>]*)|bgcolor=([^\s>]*)‘si",        "‘&#(\d+);‘e",    );        $replace = array("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "","", "", "", "", "chr(\\1)");    $show_content = preg_replace($search, $replace, $show_content);    $show_content = preg_replace("/style=[‘\"]*([^‘\"]*)[‘\"]*/is", "", $show_content);    $show_content = str_replace("\r\n", "", $show_content); //去除HTML中的分行符號&nbsp;    $show_content = str_replace("<span", "", $show_content); //去除HTML中的分行符號&nbsp;    $show_content = str_replace("&nbsp;", "", $show_content); //去除HTML中的分行符號    $show_content = str_replace(" ", "", $show_content); //去除HTML中的分行符號    return $show_content;    }//中文字元截取函數function cnsubstr($string, $sublen ,$a=0){    if ($string == ‘‘){        return false;    }    $string = changeYinhaoCom($string,‘out‘);    $sum = 0;    for ($i = 0 ; $i < strlen($string) ; $i++){        if(ord($string{$i}) > 127) {            $s .= $string{$i} . $string{++$i} . $string{++$i};            $sum++;            continue;        } else {            $s .= $string{$i};            $sum++;            continue;        }    }    if($sublen >= $sum) {        return $string;    }    $s = "";    $index = 0;    for($i = 0; $i < $sublen; $i++) {        if(ord($string{$index}) > 127) {            $s .= $string{$index} . $string{++$index} . $string{++$index};            $index++;            continue;        } else {            $s .= $string{$index};            $index++;            continue;        }    }    if($a===0){        $s.="...";    }else{        $s.=$a;    }    return $s;}function changeYinhaoCom($content , $flag){    if($flag=="in"){        $content  = str_replace("\"","&quot;",$content);        $content  = str_replace("‘","&#39;",$content);    }    if($flag=="out"){        $content  = str_replace("&quot;","\"",$content);        $content  = str_replace("&#39;","‘",$content);    }    return $content;}//調用方法$str = ‘這是一段很長的字串我要把它截取一部分‘;echo cnsubstr(content_replace($str),5);echo $res;

 

編碼-截取中文-去除HTML字元

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.