假设我有这个丑陋的字符串:
$uglyString = 'thing.header.setLoadBaseData(true);</script><script type=\'text/javascript\'>window.preload={"serverVersion":"20161019","locale":"en_US","token":{"value":"aAdcDqpeWcaJzmmWeH9ElQ:1477340771943","valid":43200000},"accounts":[{"id":"55153549","datasetId":"a55153549w0p0","s4id":"FU\/llMk3qzAP6ClSIHqMU73huAo=","name":"Ga abd Gub","isPremium":false,"starred":false,"totalNumberOfProfiles":1,"wprops":[{"id":"88276407","datasetId":"a55153549w88276407p0","s4id":"T1xfVTHwcwLUwkUSlzEfZrRYL3E=","name":"GrubHub Preview","number":1,"configuredForAppData":false,"configuredForWebData":true,"isPr';
从上面的字符串中,我需要使用PHP提取值aAdcDqpeWcaJzmmWeH9ElQ:1477340771943
。
它位于token
旁边。令牌将始终显示为"token":{"value":"aAdcDqpeWcaJzmmWeH9ElQ:1477340771943"
这就是我所拥有但不起作用
preg_match('/(?<=token":{"value":)(.*)(?=.*)/', $uglyString, $matches);
答案 0 :(得分:3)
使用常量并捕获将发生变化的内容:
preg_match('/"token":{"value":"([^"]+)"/', $uglyString, $matches);
echo $matches[1];
所以匹配所有常量内容直到"
,然后捕获所有非"
的内容,直到最后"
。