开发者

parse javascript file with php

开发者 https://www.devze.com 2023-02-18 08:33 出处:网络
i tried to use the following and it didn\'t return all of the JavaScript. $homepage = file_get_contents(\'dr_0702/.js\');

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 开发者_如何学Ci 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; }

}
0

精彩评论

暂无评论...
验证码 换一张
取 消