【问题标题】:How to "use" HTML special chars in PHP function like strpos?如何在 strpos 等 PHP 函数中“使用”HTML 特殊字符?
【发布时间】:2016-01-31 13:56:43
【问题描述】:

我遇到的问题是,我不能将 substr、strpos 等 php 字符串函数与中间点等 HTML 特殊字符一起使用。

我的具体问题:

$tdp = gettexts('TDP: ' , '•' , $complete_info);

给我一​​个文本片段的函数:

function gettexts ($startst, $endst, $content){
    $first_step = explode($startst , $content);
    $second_step = explode($endst , $first_step[1]);
    $textst= $second_step[0];
    return $textst;
}

不起作用。 我该如何解决这个问题?

编辑: 当我用这段代码测试它时它可以工作:

$turbo = gettexts('Turbo: ' , '•' , 'Turbo: 4.70GHz • TDP: 220W • Fertigung: 32nm •');

这是我要读出的页面: http://skinflint.co.uk/intel-core-i7-6700t-cm8066201920202-a1261888.html

这里有一个完整的测试代码。 turbo 频率的结果应该是 3.60(而且我不能使用 Ghz,因为有时它的 Turbo: N/A 我真的很想用这些点来爆炸;)

<?php
$content = file_get_contents('http://geizhals.eu/intel-core-i7-6700t-cm8066201920202-a1261888.html');
$complete_info= strip_tags(gettexts('<div id="gh_proddesc">' ,'Gelistet seit:' , $content));
var_dump($complete_info);
echo '<br><br>';
function gettexts ($startst, $endst, $content){
    $first_step = explode($startst , $content);
    $second_step = explode($endst , $first_step[1]);
    $textst= $second_step[0];
    return $textst;
}
echo 'Frequency:'. $frequency = gettexts('Taktfrequenz: ' , 'GHz' , $complete_info);
echo '<br>';
echo 'Turbo-Frequency:'.$turbo = gettexts('Turbo: ' , '•' , $complete_info);
?>

我没有找到允许读取 URL 的代码共享站点,但 http://phpfiddle.org/ 允许(不共享)。

【问题讨论】:

  • 你能发一份你的 $complete_info 样本吗?
  • complete_info = 八核:“Vishera” • 节拍频率:4.40GHz,Turbo:4.70GHz • TDP:220W .....
  • 在我的回答中添加了工作代码

标签: php html htmlspecialchars


【解决方案1】:

已编辑:

所以你正在报废一个页面并想要提取一些信息。 如果您复制粘贴,我以前的代码可以工作,但是要获取网页存在编码问题(该页面是 cp1252 编码但没有标题)。

您应该解析 dom(在修复编码标头之后)并使用 xpath 来提取内容...但是为了根据您的代码快速修复,只需删除 strip_tags 并使用我的函数。

查看下载页面前后的源码,你会发现如果使用strip_tags,htm实体就没有了。

这将起作用:

function gettexts ($startst, $endst, $content){
    $first_step = explode(html_entity_decode($startst) , html_entity_decode($content));
    $second_step = explode(html_entity_decode($endst), $first_step[1]);
    $textst= $second_step[0];
    return $textst;
}

$content = file_get_contents('http://geizhals.eu/intel-core-i7-6700t-cm8066201920202-a1261888.html');
$string = gettexts('<div id="gh_proddesc">' ,'Gelistet seit:' , $content);

echo 'Frequency:'. $frequency = gettexts('Taktfrequenz: ' , 'GHz' , $string);
echo '<br>';
echo 'Turbo-Frequency:'.$turbo = gettexts('Turbo: ' , '&#149;' , $string);

【讨论】:

  • 对我不起作用。未检测到中间点。
  • 我尝试过,但结果相同。我在我的初始帖子中添加了一个测试代码。带有原始网址。当它起作用时,我接受解决方案:D
  • 工作!非常感谢。但我现在使用 html 解析器,但它也只能使用 '•'所以你的解决方案非常有帮助。非常感谢。
猜你喜欢
  • 2014-08-03
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 2012-01-25
相关资源
最近更新 更多