Fox Sank
Fox Sank

Reputation: 25

javascript variable parsing by php regex

I am trying to parse some javascript variables using PHP.

Random website: (not my website)

//.... content ...
    function(response) {
        if (response) {
            var data = { id: "fka1q021", action: "post", date: "today" };
        }
        //.....

$url = 'www.website.com';
$content = htmlspecialchars(file_get_contents($url));
    preg_match_all('/var data = { id: "(.*?)", action: "post", date: "today" };/', $content, $matches);
    print_r($matches);

Output:

Array ( [0] => Array ( ) [1] => Array ( ) )

I want to get value of id which is fka1q021

How can I get it? Thanks

Upvotes: 1

Views: 2078

Answers (5)

Alvin S. Lee
Alvin S. Lee

Reputation: 5182

You may want to try preg_match instead of preg_match_all. Then, the $matches array will contain the entire matched expression as its 0th element, and the match within your parenthesized sub-expression as its 1st element. That 1st element is the id you're looking for.

So, try:

preg_match('/var data = { id: "(.*?)", action: "post", date: "today" };/', $content, $matches);

Here is full example code:

<?php

$content = <<<ENDOFCONTENT
//.... content ...
  function(response) {
    if (response) {
      var data = { id: "fka1q021", action: "post", date: "today" };
    }
//.....
ENDOFCONTENT;

preg_match('/var data = { id: "(.*?)", action: "post", date: "today" };/', $content, $matches);
print_r($matches);
print "id = " . $matches[1] . "\n";

Here is resulting output:

Array
(
    [0] => var data = { id: "fka1q021", action: "post", date: "today" };
    [1] => fka1q021
)
id = fka1q021

Upvotes: 2

C3roe
C3roe

Reputation: 96413

You are not getting anything with your regular expression, because you are applying htmlspecialchars to the content you are reading – and that replaces " with &quot;, so your expression does not find the " you have in there.

Just remove htmlspecialchars, and your pattern will match.

Upvotes: 1

Nick Humrich
Nick Humrich

Reputation: 15785

A very simple regex is

(?:id:\s(\".*?\"))

Regular expression visualization

Debuggex Demo

then the id will be in capture group 1.

Let me know if you need a more complex regex. And if so, provide more info.

Upvotes: 0

Pierre Granger
Pierre Granger

Reputation: 2010

I'm not sure what your doing is legit... JS ans PHP doesn't execute at the same time and in the same place. Maybe you got this after an ajax call ?

Anyway, analysing a JS data with php can be done with json_decode. I think your problem here will be that json_decode won't understand {id:"value"}, he needs {"id":"value"} instead...

if ( preg_match("#data = ({[^}]*})#",$data,$matches) )
    json_decode($matches[1]) ;

For example,

print_r(json_decode('{"id":"fka1q021","action":"post","date":"today"}') );

Render a php object :

stdClass Object
(
    [id] => fka1q021
    [action] => post
    [date] => today
)

Upvotes: 0

markatch
markatch

Reputation: 33

Javascript is client side and PHP is server side, so all the PHP will have executed before the Javascript can run. See this question: How to pass JavaScript variables to PHP?

Upvotes: 0

Related Questions