简体   繁体   中英

parse javascript file with php

i tried to use the following and it didn't return all of the JavaScript.

$homepage = file_get_contents('dr_0702/.js');

echo $homepage;

I am trying to return the JavaScript source so i can parse some of its contents. This is what i want to parse:

but from this code i only want to parse msgTitle, msgBody, insertDate

I put this function together for a purpose just like this. My function looks for a unique starting string, in this case something like msgTitle":" and then it grabs every character until it runs into the ending string, which is a double quote in this case.

To use it you would execute these PHP statements:

$msgTitle = parse_to_string('msgTitle":"', '"', $homepage);

$msgBody = parse_to_string('msgBody":"', '"', $homepage);

$insertDate = parse_to_string('insertDate":"', '"', $homepage);

Here is the PHP function

function parse_to_string($beginning_string, $ending_string, $custom_string='')
{

    // . in Regular Expressions means match any character
    // * in Regular Expressions means "greedy" and grab everything up to the ending string
    // siU in Regular Expressions means ignore case sensitivity

    if('' != $custom_string){

        // Search $html variable for all characters between the begin and end strings 
        // INCLUDING the begin and end strings  
        preg_match_all("($beginning_string.*$ending_string)siU", $custom_string, $matching_data);


            return $matching_data[0][0];

    }
            else { return false; }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM