Wondering Coder
Wondering Coder

Reputation: 1702

How to retrieve a value from js file?

Basically my question is How do you get the value of a variable inside a js file.

E.g

var now = (new Date() - 0);

other_var = 'Whats up';//how to pull the value of the other_var which is 'Whats Up'

key.embed();

How do get the value of the other_var using php? I only need the value of the variable which is 'whats up'.

Made some digging on my own, now I am able to get the content of the js file using file_get_content function in php, just don't know how to pull get the variable and pull its value.

Upvotes: 0

Views: 120

Answers (3)

DaveRandom
DaveRandom

Reputation: 88647

<?php

  $file = file_get_contents('myfile.js');

  $varNameToFind = 'other_var';

  $expr = '/^\s*(?:var)?\s*'.$varNameToFind.'\s*=\s*([\'"])(.*?)\1\s*;?/m';

  if (preg_match($expr, $file, $matches)) {
    echo "I found it: $matches[2]";
  } else {
    echo "I couldn't find it";
  }

Example

Something like that? Note that it would only find string values as it looks for quotes, and there are various holes in it that allow it to match a few things that are syntactically invalid Javascript and it will fall down when there are escaped quotes in the string - but as long as the JS is valid, that should find the first place in the file where a string value is assigned to the named variable, with or without a var keyword.

EDIT

A much better version that only matches syntactically valid Javascript strings and should match any valid single strings including those with escaped quotes, although it still won't handle concatenation expressions. It also gets the actual value of the string as it would be when loaded into Javascript - i.e. it interpolates escape sequences as defined here.

Upvotes: 1

luhfluh
luhfluh

Reputation: 484

If I assume the file contents is as you described:

    var now = (new Date() - 0);

other_var = 'Whats up';//how to pull the value of the other_var which is 'Whats Up'

key.embed();

then I can suggest you use the following:

    $data = file_get_contents("javascriptfile.js"); //read the file
//create array separate by new line
//this is the part where you need to know how to navigate the file contents 
//if your lucky enough, it may be structured statement-by-statement on each
$contents = explode("\n", $data); 
$interestvar = "other_var";
$interestvalue = "";
foreach ($contents as $linevalue)  
{
    //what we are looking for is :: other_var = 'Whats up';
    //so if "other_var" can be found in a line, then get its value from right side of the "=" sign
    //mind you it could be in any of the formats 'other_var=xxxxxx', 'other_var= xxxxxx', 'other_var =xxxxxx', 'other_var = xxxxxx', 
    if(strpos($linevalue,$interestvar." =")!==false){
        //cut from '=' to ';'
        //print strpos($linevalue,";");
        $start = strpos($linevalue,"=");
        $end = strpos($linevalue,";");
        //print "start ".$start ." end: ".$end;
        $interestvalue = substr($linevalue,$start,$end-$start);
        //print $interestvalue;
        break;
    }
}
if($interestvalue!=="")
print "found: ".$interestvar. " of value : ".$interestvalue;

Upvotes: 1

Willem Mulder
Willem Mulder

Reputation: 13994

Just look up "other_var = " and then check what comes after it... Get the file with

$content = file_get_contents(...);

Upvotes: 1

Related Questions