Home > Backend Development > PHP Tutorial > php过滤掉无法识别的问号字符解决方案

php过滤掉无法识别的问号字符解决方案

WBOY
Release: 2016-06-13 11:48:46
Original
1696 people have browsed it

php过滤掉无法识别的问号字符
有一个文件,txt打开后有几处显示问号
salisbury rd ?open ?requested CONTRAC
网页上也显示问号,意思应该是不识别

想把这些不识别的字符过滤掉。
试过了几个方法都不行

$str = file_get_contents('example_text.txt');<br />echo $str;
Copy after login


------解决方案--------------------
试试用这个方法过滤下。

<br />function filter_utf8_char($ostr){<br />    preg_match_all('/[\x{FF00}-\x{FFEF}<br><font color='#FF8000'>------解决方案--------------------</font><br>\x{0000}-\x{00ff}<br><font color='#FF8000'>------解决方案--------------------</font><br>\x{4e00}-\x{9fff}]+/u', $ostr, $matches);<br />    $str = join('', $matches[0]);<br />    if($str==''){   //含有特殊字符需要逐個處理<br />        $returnstr = '';<br />        $i = 0;<br />        $str_length = strlen($ostr);<br />        while ($i<=$str_length){<br />            $temp_str = substr($ostr, $i, 1);<br />            $ascnum = Ord($temp_str);<br />            if ($ascnum>=224){<br />                $returnstr = $returnstr.substr($ostr, $i, 3);<br />                $i = $i + 3;<br />            }elseif ($ascnum>=192){<br />                $returnstr = $returnstr.substr($ostr, $i, 2);<br />                $i = $i + 2;<br />            }elseif ($ascnum>=65 && $ascnum<=90){<br />                $returnstr = $returnstr.substr($ostr, $i, 1);<br />                $i = $i + 1;<br />            }elseif ($ascnum>=128 && $ascnum<=191){ // 特殊字符<br />                $i = $i + 1;<br />            }else{<br />                $returnstr = $returnstr.substr($ostr, $i, 1);<br />                $i = $i + 1;<br />            }<br />        }<br />        $str = $returnstr;<br />        preg_match_all('/[\x{FF00}-\x{FFEF}<br><font color='#FF8000'>------解决方案--------------------</font><br>\x{0000}-\x{00ff}<br><font color='#FF8000'>------解决方案--------------------</font><br>\x{4e00}-\x{9fff}]+/u', $str, $matches);<br />        $str = join('', $matches[0]);<br />    }<br />    return $str;<br />}<br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template