PHP-带有双引号字符串的正则表达式


PHP - Regular Expressions with Double Quoted Strings

我正试图使用正则表达式从字符串中提取某个值:

"exec_hash":"/TPPE2ChB+5HuSHs84FBgx5/EgWi0OlaEXoXq4pq3Aukhc1Ypf0mZfKCJ10w=","events_collector":"thiiM0ahsieSiech1hithe6chahnoo8sah6aid''n"

我想要的数据是引号之间的散列。问题是字符串中有多个引号,并且preg_match_all函数没有返回正确的数据。我已经用regex玩了一段时间,但还是弄不明白。最终,我希望将这些数据返回到一个值中。例如:$string1 = '/TPPE2ChB+5HuSHs84FBgx5/EgWi0OlaEXoXq4pq3Aukhc1Ypf0mZfKCJ10w=';

更正:我正在使用curl来抓取页面内容。数据没有存储在变量中。

$matches=array();$thing=preg_match_all('/hash":"(.*?)","events/',$page,$matches);print_r($matches);

它抛出了一个长数组,其中包含的不仅仅是散列

你能使用substr吗?

还没有测试过,但理论上。。。

$pos_of_comma = strpos($str, ",");
if($pos_of_comma !== false) {
    $execHash = substr($str, 14, $pos_of_comma - 14);
}

看起来它是json_decodable的:

//Get the result from curl request:
$curlResult = '"exec_hash": "/TPPE2ChB+5HuSHs84FBgx5/EgWi0OlaEXoXq4pq3Aukhc1Ypf0mZfKCJ10w=", "events_collector": "thiiM0ahsieSiech1phithe6chahngoo8sah6aid'n"';
//Parse the result:
$parsedResult = json_decode($curlResult, true);
//Get the hash:
$hash = $parsedResult["exec_hash"];

感谢您的建议。

我想明白了。我漏掉了表达式中的转义分隔符。

preg_match_all("/exec_hash''":''"(.*?)''",/",$page,$matches);