问题是这样的,我的好友测试发现使用以下代码未将 html 中的 替换成 1。
$str = 'abc yaya';
$doc = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadHTML("Test ");
$doc->loadHTML("<html><body>Test </body></html>");
$res = $doc->textContent;
$res = str_replace(' ', '1', $res);
查找问题
$space = sub_str($res, 4);
echo ord($splace);
结果返回ASCII值为: 194 (非正常空格!) 正常空格的ASCII码值为 32 。
问题出在 DOMDocument->loadHTML 上,具体未再深究...
解决问题
- 方法一
将特殊空格符号转换成$res = htmlentities($res); ,在进行coding... - 方法二
直接替换掉特殊字符,我在替换掉$res = str_replace(array(chr(194), chr(160)), '', $res);194之后发现还有问题,再次查找发现 是由ASCII(194 + 160)组成, 那就一起干掉吧...









网友评论