Jump to content

Get string after = sign


mouseywings

Recommended Posts

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!

Link to comment
Share on other sites

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]'];
Link to comment
Share on other sites

 

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 by mouseywings
Link to comment
Share on other sites

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 by mouseywings
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.