简体   繁体   English

preg_replace正在替换所有内容

[英]preg_replace is replacing everything

I want to replace certain html tags with null string and want to retrieve text only. 我想用空字符串替换某些html标签,并且只想检索文本。 Below is the example that I want. 下面是我想要的示例。

preg_match_all("/<span id=\"priceblock_ourprice\" class=\"a-size-medium a-color-price\">(.*)<\/span>/U", $content, $matches);

The above line retrieves something like this. 上面的行检索类似这样的内容。

<span id="priceblock_ourprice" class="a-size-medium a-color-price">50</span>

Now, I want to retrieve the integer value only (ie 50). 现在,我只想检索整数值(即50)。 I tried the following statement to remove the HTML tags. 我尝试了以下语句来删除HTML标记。

    foreach($matches[0] as $key=>$val) {
        $price = preg_replace( '/<(.*)>/', '', $val);
    }

But the problem is, it replaces everything, and a null string is returned. 但是问题是,它替换了所有内容,并返回了空字符串。 It should return 50, no the null. 它应该返回50,没有null。 The output file $price variable should be like: 输出文件$ price变量应类似于:

$price = 50

Try adding a question mark to your regular expression 尝试在正则表达式中添加问号

foreach($matches[0] as $key=>$val) {
  $price = preg_replace( '/<(.*?)>/', '', $val);
}

This will have the effect of finding the first > instead of the last one. 这将具有查找第一个>而不是最后一个的效果。 Regular expressions are greedy and will find everything it can. 正则表达式很贪婪,会找到所有可能的东西。

Also, keep in mind that the way you are doing this will replace $price with each loop. 另外,请记住,您执行此操作的方式将用每个循环替换$ price。 I am assuming you're doing something with $price before the next loop occurs, but if not, you should store the price in an array. 我假设您在下一个循环发生之前用$ price做某事,但是如果没有,则应该将价格存储在数组中。

If it seems to match more than expected use ? 如果看起来比预期的用途更多? for a non greedy match. 进行非贪婪的比赛。 Greedy ( .* ) will consume as much as possible, while making it non greedy ( .*? ) will prevent this. 贪婪( .* )将消耗尽可能多的东西,而使其变得不贪婪( .*? )将防止这种情况的发生。

preg_replace('/<(.*?)>/', '', $val);

I would consider using DOM for this also, below is an example. 我也会考虑为此使用DOM ,下面是一个示例。

$content = <<<DATA
<span id="priceblock_ourprice" class="a-size-medium a-color-price">50</span>
<span id="priceblock_ourprice" class="a-size-medium a-color-price">40</span>
<span id="foo">30</span>
DATA;

$doc = new DOMDocument();
$doc->loadHTML($content); // Load your HTML content

$xpath = new DOMXPath($doc);
$vals = $xpath->query("//span[@id='priceblock_ourprice']");

foreach ($vals as $val) {
   echo $val->nodeValue . "\n";
}

Output 产量

50
40

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM