Regex在PHP中使用preg_match_all获取javascript函数调用


Regex to get javascript function calls using preg_match_all in PHP

我向返回javascript的服务器发出curl请求,并且我需要将每个addNewEvent函数返回到PHP数组中。所以我想我会用preg_match_all。

不幸的是,regex不是我的强项,而且我一直在犯错。有人能帮我一下吗?

到目前为止,我有这个:

'/addNewEvent'('{(.*)'}');/s'

这是javascript中的一个片段,包含我需要的函数。javascript的其余部分确实包含带大括号的if语句:

addNewEvent({id:2, type:1,
start: new Date(2012,02-1,10,09,00),
end:new Date(2012,02-1,10,10,00), 
startRounded:new Date(2012,1,10,9,0),
endRounded:new Date(2012,1,10,10,0),
desc:"test", allDay:0, recurring:0, reminder:0,
comment:"this is a tes''''st'''"",
labelColor:''#FF9281'', numOverlaps:0, overlapNumber:0});
addNewEvent({id:3, type:1,
start: new Date(2012,02-1,10,09,00),
end:new Date(2012,02-1,10,10,00), 
startRounded:new Date(2012,1,10,9,0),
endRounded:new Date(2012,1,10,10,0),
desc:"test 2", allDay:0, recurring:0, reminder:0,
comment:"",
labelColor:''#849CE7'', numOverlaps:0, overlapNumber:0});

提前谢谢。

编辑13:23

理想情况下,我希望从javascript函数调用中返回一个包含JSON对象的数组:

array(
    [0] => '{id:2, type:1,
        start: new Date(2012,02-1,10,09,00),
        end:new Date(2012,02-1,10,10,00), 
        startRounded:new Date(2012,1,10,9,0),
        endRounded:new Date(2012,1,10,10,0),
        desc:"test", allDay:0, recurring:0, reminder:0,
        comment:"this is a tes''''st'''"",
        labelColor:''#FF9281'', numOverlaps:0, overlapNumber:0}',
    [1] => '{id:3, type:1,
        start: new Date(2012,02-1,10,09,00),
        end:new Date(2012,02-1,10,10,00), 
        startRounded:new Date(2012,1,10,9,0),
        endRounded:new Date(2012,1,10,10,0),
        desc:"test 2", allDay:0, recurring:0, reminder:0,
        comment:"",
        labelColor:''#849CE7'', numOverlaps:0, overlapNumber:0}'
);

下面是一个完整的测试(为了使用heredoc语法,我用双引号替换了一些单引号):

$pattern='/addNewEvent'('{(.*)'}');/sU';
$subject='addNewEvent({id:2, type:1,
start: new Date(2012,02-1,10,09,00),
end:new Date(2012,02-1,10,10,00), 
startRounded:new Date(2012,1,10,9,0),
endRounded:new Date(2012,1,10,10,0),
desc:"test", allDay:0, recurring:0, reminder:0,
comment:"this is a tes'''"st'''"",
labelColor:'"#FF9281'", numOverlaps:0, overlapNumber:0});
addNewEvent({id:3, type:1,
start: new Date(2012,02-1,10,09,00),
end:new Date(2012,02-1,10,10,00), 
startRounded:new Date(2012,1,10,9,0),
endRounded:new Date(2012,1,10,10,0),
desc:"test 2", allDay:0, recurring:0, reminder:0,
comment:"",
labelColor:'"#849CE7'", numOverlaps:0, overlapNumber:0});';
print '<pre>';
if(preg_match_all($pattern,$subject,$matches)){
    print_r($matches[1]);
}