1

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

Fox Sank
  • 25
  • 1
  • 2
  • 7

5 Answers5

2

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
Alvin S. Lee
  • 4,984
  • 30
  • 34
  • Array is empty... Output: Array ( ) – Fox Sank Feb 07 '14 at 15:49
  • I've edited my answer to include the input content (sample) that you provided, along with my resulting output. It shows that it works. Are you sure that `$content = htmlspecialchars(file_get_contents($url));` does what you think it does? It may be helpful for you to print out `$content` immediately after that line of your code, to ensure that you have the full content of the file. – Alvin S. Lee Feb 07 '14 at 15:56
  • [CBroe](http://stackoverflow.com/users/1427878/cbroe) does have a point regarding `htmlspecialchars`. His answer gets an up-vote from me. – Alvin S. Lee Feb 07 '14 at 16:07
1

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.

CBroe
  • 91,630
  • 14
  • 92
  • 150
0

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?

Community
  • 1
  • 1
markatch
  • 33
  • 6
0

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
)
Pierre Granger
  • 1,993
  • 2
  • 15
  • 21
  • He's trying to parse a JavaScript object in PHP. It's not written as valid JSON, so `json_decode` won't work, unless he can "fix" the string. – gen_Eric Feb 07 '14 at 15:39
0

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.

Nick Humrich
  • 14,905
  • 8
  • 62
  • 85