php 正则获取字符串中某段未知子串

本文介绍了一种使用PHP从特定JD商品页面抓取不同评分等级评论URL的方法,通过正则表达式匹配,获取商品名称及对应好评、中评、差评的评论页面链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php 

$i = '<a target="_blank" title="3.29-3.31iPhone11领券立减200元,特惠机型推荐:XSMax低至5799元,XR低至4199元超值推荐!" href="//item.jd.com/100008348542.html" onclick="searchlog(1,100008348542,1,2,>
<a target="_blank" title="3.29-3.31iPhone11领券立减40元,特惠机型推荐:XSMax低至5799元,XR低至4199元超值推荐!" href="//item.jd.com/100008348542.html" onclick="searchlog(1,100008348542,1,2,>';

//读文件
// $myfile = fopen("jd.html", "r") or die("Unable to open file!");
// $i =  fread($myfile,filesize("jd.html"));
// fclose($myfile);

preg_match_all('/target="_blank" title="(.*?) onclick=/m',$i,$m);
print_r($m);exit;
$d =[];
foreach ($m[0] as  $v) {
	preg_match('/title="(.*?)" href=/m',$v,$m1);
	
	preg_match('/jd.com\/(.*?).html/m',$v,$m2);
	
	$d[]=[
		'name' => $m1[1],
		'good_url' => "https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=".$m2[1]."&score=3&sortType=5",
		'mid_url' => "https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=".$m2[1]."&score=2&sortType=5",
		'bid' => "https://club.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98&productId=".$m2[1]."&score=1&sortType=5",
	];
}
print_r(json_encode($d));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值