[英]How can I get price from html using regex in php?
我需要使用正則表達式從html獲取價格。 以下是html和php代碼列表。 我嘗試了一些正則表達式代碼,但出現了一些錯誤。 請幫我。
正則表達式1:
html
<p class="productprice"><strong>from 1.416,10 EUR</strong></p>
PHP
<?php
$subject = '<p class="productprice"><strong>from 1.416,10 EUR</strong></p>';
$pattern = '<p class="productprice"><strong>from ([0-9]+.*) EUR</strong></p>';
preg_match($pattern, $subject, $tokens);
var_dump($tokens[1]);
?>
我期待的結果
1.416,10
正則表達式2:
HTML
<p class="productprice">
<strong>
<span class="productOldPrice">
<small>Our previous price </small>
<del> 1.416,10 €</del>
</span>
<br> Now only 1.299,90 €<br>
<small>you save 8 % / 116,20 €</small>
</strong>
</p>
PHP
<?php
$subject = '<p class="productprice"><strong><span class="productOldPrice"><small>Our previous price </small><del> 1.416,10 €</del></span><br> Now only 1.299,90 €<br><small>you save 8 % / 116,20 €</small></strong></p>';
$pattern = '<p class="productprice"><strong><span class="productOldPrice"><small>Our previous price </small><del> 1.416,10 €</del></span><br> Now only ([0-9]+.*) €<br><small>you save 8 % / 116,20 €</small></strong></p>';
preg_match($pattern, $subject, $tokens);
var_dump($tokens[1]);
?>
我期待的結果
1.299,90
錯誤
Warning: preg_match(): Unknown modifier '<' in D:\xampp\htdocs\test\test.php on line 8
NULL
正則表達式模式必須以/
開頭和結尾,后跟可選參數。 另外,不要忘記在模式中轉義所有特殊字符。
$pattern = '/<p class="productprice"><strong>from ([0-9]+.*) EUR<\/strong><\/p>/';
您可以使用Regexr之類的東西來測試您的模式。 我在這里為您創建了一個: http : //regexr.com/3f173
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.