0

I make a curl request to a server which returns javascript and I need to get each of the addNewEvent functions returned into a PHP array. So I figured I would use preg_match_all.

Unfortunately regex is not my strong point and I keep getting it wrong. Could someone please help me out with this?

I have this so far:

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

This is a snippet from the javascript containing the functions I need. The rest of the javascript does contain if statements with braces:

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});

Thanks in advance.

Edit 13:23

Ideally I would like to have returned an array containing the JSON objects from inside the javascript function calls:

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}'
);
7
  • What do you want to extract exactly? Show us an example. Commented Feb 10, 2012 at 13:17
  • You can't do this reliably with regular expression. The language is not regular. You might be able to get a solution that works in most cases though. Consider another solution to the problem. Commented Feb 10, 2012 at 13:23
  • Thanks Yotaware, I had suspected so. Commented Feb 10, 2012 at 13:24
  • As long as you want to catch everything that is between ' addNewEvent('and ');', I think it's not a problem with regexp. But, beware that by default, regexp are greedy, i.e they catch the longest expression that match. So you might catch everything from the first 'addNewEvent(' to the last '});' wherever it is. You have to modify the regexp to make it ungreedy. Not sure (didn't have time to check), but try adding 'U' after the '/s'. Commented Feb 10, 2012 at 13:28
  • huelbois - Thanks, that is what I have been experiencing. However, adding the U did solve the problem and provides me with an array containing two function calls. Commented Feb 10, 2012 at 13:34

1 Answer 1

1

Here is a full test (I've replaced some single quotes by double quotes to use heredoc syntax) :

$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]);
}    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.