如何在像strpos这样的PHP函数中“使用” HTML特殊字符?

问题描述 投票:0回答:1

我有一个问题,我无法将substr,strpos等php字符串函数与诸如middot的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/允许它(不共享)。

php html htmlspecialchars
1个回答
0
投票

编辑:

因此,您正在抓取页面,并希望提取一些信息。如果您复制粘贴,则我以前的代码有效,但是要获取网页,则存在编码问题(该页面经过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);
© www.soinside.com 2019 - 2024. All rights reserved.