我在使用PHP从Javascript中提取值时遇到问题

时间:2022-11-06 22:24:12

I received the below code amongst a whole lot of HTML from a fetch in php, the code was in script tags. I am having trouble using preg_match efficiently to extract the value of hardest27 on a certain line.

我从php中获取了大量HTML中的以下代码,代码在脚本标记中。我无法有效地使用preg_match来提取某条线上的hardest27的值。

So I currently have a variable called $html that contains a whole lot of HTML which also contains the line below.

所以我目前有一个名为$ html的变量,其中包含大量HTML,其中也包含以下行。

$("<input>").attr({name: "levelReached", value: "hardest27" }).appendTo(newForm);

$(“”)。attr({name:“levelReached”,value:“hardest27”})。appendTo(newForm);

How can I get php to return me the value of levelReached?

我怎样才能让php返回levelReached的值?

1 个解决方案

#1


2  

You may try this regex over the content:

您可以在内容上尝试此正则表达式:

"levelReached"\s*,\s*value:\s*"([^"]*)"

group 1 contains your expected value.

第1组包含您的预期值。

Regex Demo

Sample Demo Source:

示例演示来源:

preg_match_all($re, $html, $matches);

foreach($matches[1] as $matchgroup)
    echo $matchgroup."\n";

#1


2  

You may try this regex over the content:

您可以在内容上尝试此正则表达式:

"levelReached"\s*,\s*value:\s*"([^"]*)"

group 1 contains your expected value.

第1组包含您的预期值。

Regex Demo

Sample Demo Source:

示例演示来源:

preg_match_all($re, $html, $matches);

foreach($matches[1] as $matchgroup)
    echo $matchgroup."\n";