"use" HTML PHP 中的特殊字符如何像 strpos 一样运行?
How to "use" HTML special chars in PHP function like strpos?
我遇到的问题是我无法将 php 字符串函数(如 substr、strpos 等)与 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/ 允许(不共享)。
已编辑:
您正在抓取一个页面并希望提取一些信息。
如果你 copy-paste,我以前的代码可以工作,但是要获取网页存在编码问题(该页面是 cp1252 编码但没有 header)。
您应该解析 dom(在修复编码 header 之后)并使用 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: ' , '•' , $string);
我遇到的问题是我无法将 php 字符串函数(如 substr、strpos 等)与 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/ 允许(不共享)。
已编辑:
您正在抓取一个页面并希望提取一些信息。 如果你 copy-paste,我以前的代码可以工作,但是要获取网页存在编码问题(该页面是 cp1252 编码但没有 header)。
您应该解析 dom(在修复编码 header 之后)并使用 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: ' , '•' , $string);