I'm trying to use regular expressions to extract a certain value from a string:
"exec_hash": "/TPPE2ChB+5HuSHs84FBgx5/EgWi0OlaEXoXq4pq3Aukhc1Ypf0mZfKCJ10w=", "events_collector": "thiiM0ahsieSiech1phithe6chahngoo8sah6aid "
The data I want is the hash between the quotation marks. The problem is that there are multiple quotes within the string and preg_match_all
function isn't returning the correct data. I've been playing around with regex for a while but can't figure it out. Ultimately, I'd like that data to be returned into a value. Ex: $string1 = '/TPPE2ChB+5HuSHs84FBgx5/EgWi0OlaEXoXq4pq3Aukhc1Ypf0mZfKCJ10w=';
Correction: I'm using curl to grab the page content. The data isn't stored in a variable.
$matches = array(); $thing = preg_match_all('/hash": "(.*?)", "events/',$page,$matches); print_r($matches);
It spits out a long array of much more than just the hash
Can you use substr?
haven't tested this, but in theory...
$pos_of_comma = strpos($str, ",");
if($pos_of_comma !== false) {
$execHash = substr($str, 14, $pos_of_comma - 14);
}
It looks like it's json_decodable:
//Get the result from curl request:
$curlResult = '"exec_hash": "/TPPE2ChB+5HuSHs84FBgx5/EgWi0OlaEXoXq4pq3Aukhc1Ypf0mZfKCJ10w=", "events_collector": "thiiM0ahsieSiech1phithe6chahngoo8sah6aid
"';
//Parse the result:
$parsedResult = json_decode($curlResult, true);
//Get the hash:
$hash = $parsedResult["exec_hash"];
Thank you for the suggestions.
I figured it out. I missed the escape delimiters in the expression.
preg_match_all("/exec_hash\": \"(.*?)\", /",$page,$matches);