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
https://forums.phpfreaks.com/topic/291555-get-string-after-sign/
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]'];

 

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..

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 =/

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.