php過濾掉無法識別的疑問號字元

來源:互聯網
上載者:User
php過濾掉無法識別的問號字元
有一個檔案,txt開啟後有幾處顯示問號
salisbury rd ?open ?requested CONTRAC
網頁上也顯示問號,意思應該是不識別

想把這些不識別的字元過濾掉。
試過了幾個方法都不行
$str = file_get_contents('example_text.txt');
echo $str;

分享到: 更多

\x{0000}-\x{00ff}試試用這個方法過濾下。


function filter_utf8_char($ostr){
preg_match_all('/[\x{FF00}-\x{FFEF}\x{0000}-\x{00ff}\x{4e00}-\x{9fff}]+/u', $ostr, $matches);
$str = join('', $matches[0]);
if($str==''){ //含有特殊字元需要逐個處理
$returnstr = '';
$i = 0;
$str_length = strlen($ostr);
while ($i<=$str_length){
$temp_str = substr($ostr, $i, 1);
$ascnum = Ord($temp_str);
if ($ascnum>=224){
$returnstr = $returnstr.substr($ostr, $i, 3);
$i = $i + 3;
}elseif ($ascnum>=192){
$returnstr = $returnstr.substr($ostr, $i, 2);
$i = $i + 2;
}elseif ($ascnum>=65 && $ascnum<=90){
$returnstr = $returnstr.substr($ostr, $i, 1);
$i = $i + 1;
}elseif ($ascnum>=128 && $ascnum<=191){ // 特殊字元
$i = $i + 1;
}else{
$returnstr = $returnstr.substr($ostr, $i, 1);
$i = $i + 1;
}
}
$str = $returnstr;
preg_match_all('/[\x{FF00}-\x{FFEF}\x{4e00}-\x{9fff}]+/u', $str, $matches);
$str = join('', $matches[0]);
}
return $str;
}
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.