繁体   English   中英

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

[英]PHP - Regular Expressions with Double Quoted Strings

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

“ exec_hash”:“ / TPPE2ChB + 5HuSHs84FBgx5 / EgWi0OlaEXoXq4pq3Aukhc1Ypf0mZfKCJ10w =”,“ events_collector”:“ thiiM0ahsieSiech1phithe6chahngoo8sah6aid \\ n”

我想要的数据是引号之间的哈希。 问题在于字符串中有多个引号,并且preg_match_all函数未返回正确的数据。 我使用正则表达式已经有一段时间了,但无法弄清楚。 最终,我希望将这些数据返回一个值。 例如: $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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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