mouseywings Posted October 10, 2014 Share Posted October 10, 2014 So this is how the data is returned for the API I'm using: REC INFO[p!]=p EXP DATE[p43]=03-20-15 PCODE1[p44]=1 PCODE2[p45]=u So I'm trying to filter on either of the keys like 'PCODE2[p45]' or 'PCODE1[p44]'.. I'm trying to get the value after the equal sign, but still remain on the same line. So if I were searching by the p45 key, I would get 'u'. If I was searching by the p44 key I would get 1.. Is there a simple regex or something that would allow me to achieve these results? Thank you for any help! Quote Link to comment https://forums.phpfreaks.com/topic/291555-get-string-after-sign/ Share on other sites More sharing options...
ginerjm Posted October 10, 2014 Share Posted October 10, 2014 If what you are saying (and this is a guess since you aren't very clear) is that the 4 lines above represent key=value pairs, simply explode each line on the = sign and then add the 2 array elements to an array as in : list($k,$v) = explode("=",$line); $arr[$k] = $v; Of course this will have to go into some kind of loop to collect all the data (4 lines). Now you can simply interrogate the array for the value you want: $val = $arr['PCODE[p45]']; Quote Link to comment https://forums.phpfreaks.com/topic/291555-get-string-after-sign/#findComment-1493233 Share on other sites More sharing options...
mouseywings Posted October 10, 2014 Author Share Posted October 10, 2014 (edited) If what you are saying (and this is a guess since you aren't very clear) is that the 4 lines above represent key=value pairs, simply explode each line on the = sign and then add the 2 array elements to an array as in : list($k,$v) = explode("=",$line); $arr[$k] = $v; Of course this will have to go into some kind of loop to collect all the data (4 lines). Now you can simply interrogate the array for the value you want: $val = $arr['PCODE[p45]']; The problem is is that it's taking the break lines into consideration... This is what it returns: die(var_dump($k)) returns this: string '<HTML><BODY> REC INFO[p!]' (length=25) And die(var_dump($v)) returns this: string 'p<BR> EXP DATE[p43]' (length=19) I have no idea what it's even doing. I just have 4 lines. And sometimes it could be more. I just want to get the value of a key. Sometimes I'll get the p45 one, sometimes I'll need the p44. Or whatever other key I'll need in the list. When I put the returned api string in a die() method, it returns this: <body> REC INFO[p!]=p<br> EXP DATE[p43]=03-20-15<br> PCODE1[p44]=1<br> PCODE2[p45]=u<br> </body> That's the HTML at least.. Edited October 10, 2014 by mouseywings Quote Link to comment https://forums.phpfreaks.com/topic/291555-get-string-after-sign/#findComment-1493235 Share on other sites More sharing options...
mouseywings Posted October 10, 2014 Author Share Posted October 10, 2014 (edited) It's not letting me edit my post for some odd reason. But after a little digging around, I got this to display so far: string ' REC INFO[p!]=p EXP DATE[p43]=03-20-15 PCODE1[p44]=1 PCODE2[p45]=u'... $doc = file_get_contents('http://IPADDRESSTOPIN/PATRONAPI/21333008706699/dump'); $doc = trim(str_replace("<BR>", "", $doc)); $doc = preg_match("/<body[^>]*>(.*?)<\/body>/is", $doc, $matches); die(var_dump($matches[1])); I tried exploding that file, but it still did the same thing with mild changes in the results. So far that's what I have. The code is on the bottom of the result. Not sure how to get what I want after that =/ Edited October 10, 2014 by mouseywings Quote Link to comment https://forums.phpfreaks.com/topic/291555-get-string-after-sign/#findComment-1493237 Share on other sites More sharing options...
ginerjm Posted October 10, 2014 Share Posted October 10, 2014 You didn't tell me how you were getting the data. I just told you how to handle the data you showed me. It's up to you to parse the input file and get it into the format you presented first. Or figure out your own method. Quote Link to comment https://forums.phpfreaks.com/topic/291555-get-string-after-sign/#findComment-1493241 Share on other sites More sharing options...
mouseywings Posted October 10, 2014 Author Share Posted October 10, 2014 I got it working like this: public function findKey( $doc, $key ) { $doc = preg_match("/<body[^>]*>(.*?)<\/body>/is", $doc, $matches); $list = explode('<BR>', $matches[1]); foreach($list as $l) { $pair = explode('=', $l); if(trim($pair[0]) == $key) { return $pair[1]; } } return null; } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/291555-get-string-after-sign/#findComment-1493243 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.