cataiin Posted August 31, 2013 Share Posted August 31, 2013 Hi guys. I have this "sentence": "key":"321313-0381","something":"value," and I want to obtain only 321313-0381. The problem is somethings differs, isn't always the same. I've tried: $start = '"key":"'; $end = '","'; preg_match("/$start(.*)$end/s", $content, $match); $result = $match[1]; echo "$result"; But unsuccessful. For $end = '\/'; too. Can I get a little help? Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/ Share on other sites More sharing options...
Irate Posted August 31, 2013 Share Posted August 31, 2013 What does it echo right now? Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/#findComment-1447517 Share on other sites More sharing options...
cataiin Posted August 31, 2013 Author Share Posted August 31, 2013 321313-0381","something":"value," Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/#findComment-1447524 Share on other sites More sharing options...
requinix Posted August 31, 2013 Share Posted August 31, 2013 That looks like JSON. $obj = json_decode("{" . the string . "}"); // if you don't have the {}s already echo $obj->key; Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/#findComment-1447530 Share on other sites More sharing options...
cataiin Posted August 31, 2013 Author Share Posted August 31, 2013 Don't work. Notice: Trying to get property of non-object in www\index.php on line 10 Probably because the string comes from external URL. Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/#findComment-1447534 Share on other sites More sharing options...
requinix Posted August 31, 2013 Share Posted August 31, 2013 Or it's not JSON. I only guessed that it might be because you didn't post the actual $content string. Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/#findComment-1447536 Share on other sites More sharing options...
cataiin Posted August 31, 2013 Author Share Posted August 31, 2013 No idea about fixing with preg_match? Or anything else... Ty! Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/#findComment-1447540 Share on other sites More sharing options...
Irate Posted August 31, 2013 Share Posted August 31, 2013 Try this... preg_match('#"key":"(.*?)"#',$content,$match); echo $match[1]; Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/#findComment-1447543 Share on other sites More sharing options...
cataiin Posted August 31, 2013 Author Share Posted August 31, 2013 Try this... preg_match('#"key":"(.*?)"#',$content,$match); echo $match[1]; More than perfect! Thanks! Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/#findComment-1447545 Share on other sites More sharing options...
.josh Posted August 31, 2013 Share Posted August 31, 2013 if you have control over the external file, you should change the format to be JSON, or else serialize the data, so that you can more easily parse it. Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/#findComment-1447552 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.