使用preg_match提取Javascript对象参数


Extracting Javascript Object parameters using preg_match

我正试图从JS对象中提取要处理的视频信息:

var playerSettings = {flashplayer: "jwplayer.flash.swf",width: 640,height: 480,
skin: "five.xml",primary: "flash",autostart: "true",abouttext: "this video",aboutlink: "www.video.com",startparam: "ec_seek",
playlist: [{sources: [{ type: "mp4", file: "http://video-media.com/T/4/I/N/T4INzPOLNGF.480.mp4?2a8283553d5cb07b981d3deae8444eb54ae8832fd5c37be54baba4c6d41e2fb5f374d06c9f5d7609a96b96ab5fec4fd487af4cde9304969fce3913c656adb1960d3c6f7ffb87649b34d8a0a87a14b2b1243663709429bdfae86a3e9521bf631393b37c2eb2c42f1e654dbeec41579aaf3f3b8075e318&150"}],"previewer.file" : "http://i-sec.video-media.com/T/4/I/N/T4INzPOLNGF-sprite.jpg"}],
plugins: {"js/videopreviewer-2.0.js": {frameWidth: "156",frameHeight: "117",columns: "1",rows: "50",
totalFrames: 50,loadOnStart: true,thumbnailsOffset: 2,tooltip: {hook: false,cornerRadius: 10,padding: 5,paddingTop: 0,paddingBottom: 6,color: "#CCCCCC"}}}};

我正在尝试获得

{ type: "mp4", file: "http://video-media.com/T/4/I/N/T4INzPOLNGF.480.mp4?2a8283553d5cb07b981d3deae8444eb54ae8832fd5c37be54baba4c6d41e2fb5f374d06c9f5d7609a96b96ab5fec4fd487af4cde9304969fce3913c656adb1960d3c6f7ffb87649b34d8a0a87a14b2b1243663709429bdfae86a3e9521bf631393b37c2eb2c42f1e654dbeec41579aaf3f3b8075e318&150"}

从上述设置

我试过的是:

preg_match_all('/sources: '[^(.*)&']/', $scriptText, $match);
var_dump($match);

但它给出了空数组。

如果您坚持使用regex,请尝试不使用^(匹配行首)和&(匹配安培数):

preg_match_all('/sources: '[(.*)']/', $scriptText, $match);

此外,

(.*)

贪婪,所以你可能会得到比你预想的更多的东西。

(.*?)

实际上会停在第一个],这可能是你想要的。我也喜欢

([^']]*)

对于这个-贪婪匹配除"]"之外的任何字符-可以更可读。