如何使用php进行preg_match和替换字符串数据

时间:2014-12-20 15:41:16

标签: php regex preg-match

您好,我需要从以下标签中获取价格,我不知道我第一次使用php中的preg函数。请解决这个

TAG_1:

<span id="hs18Price" itemprop="price" title="Price of Intex Aqua N2 Dual SIM Android Mobile Phone - White">
<span class='WebRupee'>Rs.</span>&nbsp;5999</span>

TAG_2:

<div class="video-price-txt"><big>Intex Aqua N2 Dual SIM Android Mobile Phone - White Price: Rs. 5999 </big></div>

对于tag_2我已经​​尝试了一种方法,但是它提供了整个字符串值,我只需要价格值“5999”,而不是这个我得到如下的tag_2。

方法:if(preg_match('/<div class="video-price-txt"><big>(.*?)<\/big><\/div>/',get_page($url),$matches2))

TAG_2 OUT PUT:'price' => string 'Intex Aqua N2 Dual SIM Android Mobile Phone - White Price: Rs. 5999 ' (length=68)

请建议我如何从这两个标签中获取价格数值

1 个答案:

答案 0 :(得分:0)

如果你只想要这个号码,试试这个简单的正则表达式:

preg_match( '/Rs.\s?(\d+)/', get_page( $url ), $matches );

您应该拥有$matches[1]中的值。