我想在var data = {
和A.trigger
...
$images_script = <<<EOM
P.when('A').register("ImageBlockATF", function(A){
var data = {
'colorImages': { 'initial': [{"hiRes":"https://images-na.ssl-images-amazon.com/images/I/61z4lNt%2BjZL._SL1300_.jpg","thumb":"https://images-na.ssl-images-amazon.com/images/I/31%2BSEYm%2B8QL._SS40_.jpg","large":"https://images-na.ssl-images-amazon.com/images/I/31%2BSEYm%2B8QL.jpg",
"main":{"https://images-na.ssl-images-amazon.com/images/I/61z4lNt%2BjZL._SY355_.jpg":[355,355],"https://images-na.ssl-images-amazon.com/images/I/61z4lNt%2BjZL._SY450_.jpg":[450,450],"https://images-na.ssl-images-amazon.com/images/I/61z4lNt%2BjZL._SX425_.jpg":[425,425],"https://images-na.ssl-images-amazon.com/images/I/61z4lNt%2BjZL._SX466_.jpg":[466,466],"https://images-na.ssl-images-amazon.com/images/I/61z4lNt%2BjZL._SX522_.jpg":[522,522]},"variant":"MAIN"}]},
'colorToAsin': {'initial': {}},
'holderRatio': 1.0,
'holderMaxHeight': 700,
'weblabs' : {}
};
A.trigger('P.AboveTheFold'); // trigger ATF event.
return data;
});
EOM;
我试过了
$startsAt = strpos($out, "var data = {") + strlen("var data = {");
$endsAt = strpos($out, "A.trigger", $startsAt);
$result = substr($out, $startsAt, $endsAt - $startsAt);
也尝试了
preg_match('~var data =(.*?)A.trigger~', $images_script, $output);
但我无法获得那个JSON。
有人可以告诉我该怎么做?
答案 0 :(得分:1)
你非常接近preg_match尝试,你只需使用s
标志(单行标志)
preg_match('~var data =(.*?)A.trigger~s', $images_script, $output);
Here -----^
<强> Working demo 强>
<强> IDEOne demo 强>
顺便说一句,请记住,自从您使用;
作为边界后,捕获的json将包含A.trigger
答案 1 :(得分:-3)
如果您的数据始终相同,则可以使用下面的简单正则表达式
但是如果您的网站正在发生变化,或者有更多不同的网页,那么您必须使用其他方式
如果数据var结构总是相同:
preg_match('/\s?data\s?\=\s?(\{[^\;]+\})/i',$images_script,$matches);
$parsed=json_decode(str_replace("'",'"',$matches[1]),true);
o如果您只想要具有相应分辨率的图像,请尝试this
\"([^\"]+)\"\s?\:\s?\"(https?\:\/\/[^\"]+)\"
$mathes=[];
preg_match_all('/\"([^\"]+)\"\s?\:\s?\"(https?\:\/\/[^\"]+)\"/im',$your_text,$matches);