【发布时间】:2015-02-04 07:59:24
【问题描述】:
我正在使用 simplehtmldom 来获取一些网站数据
$data = array();
$html = file_get_html('http://www.example.com/'.$value, false, $context);
foreach($details as $value){
$dataele = array();
foreach($html->find('*[class=style11]') as $element){
$houseinfo = trim($element->plaintext, " \t\n\r\0\x0B\xC2\xA0");
echo $houseinfo;
echo '<br>';
array_push($dataele, $houseinfo);
}
}
但是当我将这些数据插入数据库时,我发现有一些&nbsp;。
我尝试了不同的方法,但它不能真正删除&nbsp; html 标记。我尝试过的方法:
$houseinfo = trim($element->plaintext, " \t\n\r\0\x0B\xC2\xA0");
$dataele[1] = html_entity_decode($dataele[1]);
$dataele[1] = str_replace(" ", "_", $dataele[1]);
$houseinfo = filter_var($houseinfo, FILTER_SANITIZE_STRING);
$dataele[1] = preg_replace("/&#?[a-z0-9]+;/i", "", $dataele[1]);
【问题讨论】:
-
Note: You might wonder why trim(html_entity_decode('&nbsp;')); doesn't reduce the string to an empty string, that's because the '&nbsp;' entity is not ASCII code 32 (which is stripped by trim()) but ASCII code 160 (0xa0) in the default ISO 8859-1 encoding. -
@Class 你能告诉我如何让它工作吗?
-
如果您使用诸如
urlencode之类的方法回显HTML,您将能够看到隐藏了哪些字符(如果有的话),以及为什么您的东西没有被替换.. -
@h2ooooooo 非常感谢,我已经找到问题所在了。我刚刚发现那是
&nbsp,而不是&nbsp;...
标签: php preg-replace trim simple-html-dom