Home  >  Article  >  php教程  >  PHP实例函数:获取淘宝商品价格

PHP实例函数:获取淘宝商品价格

WBOY
WBOYOriginal
2016-06-13 11:40:531797browse

这几天在看正则表达式的时候突然觉得正则表达式非常有意思啊,做了很多有意思的东西。突发奇想的觉得可以用正则表达式获取淘宝宝贝页面中的价格,所以就做了这一个函数了,调用时只需要填写入淘宝的链接就可以返回当前宝贝页面中的价格了,这的是非常的方便了。

function taoBaoJiaGe($url){
    preg_match('/<strong id="J_StrPrice" >\d+.\d{2}/',file_get_contents($url),$jiaGe); //正则表示获取包含价格的 HTML 标签
    preg_match('/\d+.\d{2}/',$jiaGe[0],$jiaGe); //正则表达式再次从结果中分离价格。
    return $jiaGe[0]; //返回价格
}

首先先在代码中引入自定义函数 taoBaoJiaGe(); 那么需要调用的时候怎么办,很简单。

taoBaoJiaGe('淘宝宝贝地址');

直接调用函数并且填写入淘宝网的宝贝地址即可。

这个函数对于商品比价,或者是价格录入等等都是非常有帮助的呢。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn