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? Quote 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? Quote 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," Quote 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; Quote 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. Quote 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. Quote 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! Quote Link to comment https://forums.phpfreaks.com/topic/281723-little-help-with-preg_match/#findComment-1447540 Share on other sites More sharing options...
Solution Irate Posted August 31, 2013 Solution Share Posted August 31, 2013 Try this... preg_match('#"key":"(.*?)"#',$content,$match); echo $match[1]; Quote 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! Quote 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. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.