我尝试使用以下代码,但它没有返回所有的 JavaScript。
$homepage = file_get_contents('dr_0702/.js');
echo $homepage;
我正在尝试返回 JavaScript 源代码,以便我可以解析其中的一些内容。这就是我要解析的内容:
但从这段代码我只想解析 msgTitle、msgBody、insertDate
最佳答案
我把这个功能放在一起就是为了这个目的。我的函数寻找一个唯一的起始字符串,在本例中类似于 msgTitle":",然后它获取每个字符,直到遇到结束字符串,在本例中为双引号。
要使用它,您需要执行这些 PHP 语句:
$msgTitle = parse_to_string('msgTitle":"', '"', $homepage);
$msgBody = parse_to_string('msgBody":"', '"', $homepage);
$insertDate = parse_to_string('insertDate":"', '"', $homepage);
这是PHP函数
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; }
}
关于php - 用php解析javascript文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5400296/