使用 SimpleHTMLDom 抓取时替换一段字符串

发布于 2024-10-21 03:39:38 字数 1701 浏览 3 评论 0原文

使用: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-10-28 03:39:38

哦,我已经解决了。我必须在 preg_replace(); 中使用反斜杠;并且必须写出特殊的字符“特殊的方式”。

<?php
// HTML ophalen
$html->load_file('http://www.xxlnutrition.nl/whey-delicious/xxl-nutrition');
$html = preg_replace('/2500 gram » /', '', $html);
$html = str_get_html($html);

$wd25kg = $html->find('option[value=437]', 0)->innertext;

// Prijs ophalen van XXL Nutrition Whey Delicious dmv <option value=*>
echo "Whey Delicious 2500 gram: " . $wd25kg;
?>

Oh, I've solved it. I had to use a backslash with preg_replace(); and had to write the special character “the special way”.

<?php
// HTML ophalen
$html->load_file('http://www.xxlnutrition.nl/whey-delicious/xxl-nutrition');
$html = preg_replace('/2500 gram » /', '', $html);
$html = str_get_html($html);

$wd25kg = $html->find('option[value=437]', 0)->innertext;

// Prijs ophalen van XXL Nutrition Whey Delicious dmv <option value=*>
echo "Whey Delicious 2500 gram: " . $wd25kg;
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文