if(is_file($url)) {
$handle = fopen($url, 'rb');
$txt_content = '';
$preg = '/^\s| +/';//去掉每段文字前面的普通空格和全角空格
//获取第一句并判断字符编码
$tmp = fgets($handle);
$code = mb_detect_encoding($tmp,array('ASCII','GB2312','GBK','UTF-8'));
rewind($handle);
if(in_array($code, array('UTF-8', 'CP936'))) {
while (! feof($handle)) {
$str = preg_replace($preg, '', fgets($handle));
$bool = preg_match('/^.*[\x4e00-\x9fa5]+.*$/', $str);
if($bool) {
$txt_content .= '<p> ' . $str . '</p>';
}
}
}else {
while (! feof($handle)) {
$str = preg_replace($preg, '', iconv($code, 'UTF-8', fgets($handle)));
$bool = preg_match('/^.*[\x4e00-\x9fa5]+.*$/', $str);
if($bool) {
$txt_content .= '<p> ' . $str . '</p>';
}
}
}
fclose($handle);
}
php---判断字符编码
最新推荐文章于 2021-04-13 16:24:16 发布