使用 SimpleHTMLDom 抓取时替换一段字符串
使用: http://simplehtmldom.sourceforge.net/
我正在比较几乎相同产品的价格不同的品牌。现在,当我从标签获取信息时,重量已经包含在内,我想删除该部分并自己编写重量,因为我将把所有信息放入表格中。
代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Supplementen Prijzenvergelijking</title>
</head>
<body>
<?php
include("simple_html_dom.php");
// DOM object creeëren
$html = new simple_html_dom();
// HTML ophalen
$html->load_file('http://bodyenfitshop.nl/whey-proteine/body-fit-sportsnutrition/whey-perfection');
$wp22kg = $html->find(".r", 2)->innertext;
$wp44kg = $html->find(".r", 4)->innertext;
// Prijs ophalen van B&F Shop Whey Perfection dmv innertext van <td class=r>
echo "Whey Perfection 2270 gram: " . $wp22kg . "<br>";
echo "Whey Perfection 4540 gram: " . $wp44kg . "<br>";
// HTML ophalen
$html->load_file('http://www.xxlnutrition.nl/whey-delicious/xxl-nutrition');
$wd25kg = $html->find('option[value=437]', 0)->innertext;
// Prijs ophalen van XXL Nutrition Whey Delicious dmv <option value=*>
echo "Whey Delicious " . $wd25kg;
?>
</body>
</html>
这是输出:
Whey Perfection 2270 gram: € 32,90
Whey Perfection 4540 gram: € 54,90
Whey Delicious 2500 gram » € 49.95
我想替换“2500 克 »”,我该怎么做?我尝试过 str_ireplace 和 preg_replace 但无法使其工作,输出保持不变。
注意:我是业余爱好者。
Using: http://simplehtmldom.sourceforge.net/
I'm comparing prices of pretty much the same products of different brands. Now, when I'm getting info from a tag, the weight is already included, I want to get rid of that part and write the weight myself, as I will be putting all the info in a table.
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Supplementen Prijzenvergelijking</title>
</head>
<body>
<?php
include("simple_html_dom.php");
// DOM object creeëren
$html = new simple_html_dom();
// HTML ophalen
$html->load_file('http://bodyenfitshop.nl/whey-proteine/body-fit-sportsnutrition/whey-perfection');
$wp22kg = $html->find(".r", 2)->innertext;
$wp44kg = $html->find(".r", 4)->innertext;
// Prijs ophalen van B&F Shop Whey Perfection dmv innertext van <td class=r>
echo "Whey Perfection 2270 gram: " . $wp22kg . "<br>";
echo "Whey Perfection 4540 gram: " . $wp44kg . "<br>";
// HTML ophalen
$html->load_file('http://www.xxlnutrition.nl/whey-delicious/xxl-nutrition');
$wd25kg = $html->find('option[value=437]', 0)->innertext;
// Prijs ophalen van XXL Nutrition Whey Delicious dmv <option value=*>
echo "Whey Delicious " . $wd25kg;
?>
</body>
</html>
This is the output:
Whey Perfection 2270 gram: € 32,90
Whey Perfection 4540 gram: € 54,90
Whey Delicious 2500 gram » € 49.95
I would like to replace “2500 gram » ”, how do I do it ? I've tried str_ireplace and preg_replace but couldn't get it working, output was left unchanged.
Note: I'm an amateur.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哦,我已经解决了。我必须在 preg_replace(); 中使用反斜杠;并且必须写出特殊的字符“特殊的方式”。
Oh, I've solved it. I had to use a backslash with preg_replace(); and had to write the special character “the special way”.