Find string, then find string in-between chars

2472 views php
2

I'm having a hard time with this....

So, I have a load of text:

a:13:{s:9:"live_odds";i:0;s:14:"show_matchbook";i:0;s:12:"show_betfred";i:1;s:16:"show_williamhill";i:1;s:12:"betfair_show";i:1;s:16:"betfair_username";N;s:16:"betfair_password";s:9:"";s:20:"betfair_affiliate_id";s:3:"888";

Now, what I am trying to do is search for betfair_affiliate_id";s:3: within that bulk of text and then display the text between the speech marks.

I was trying

        $betfred_show = 'betfair_affiliate_id";s:3:"';
        $betfred_show_value = substr($string, strpos($string, $betfred_show) + strlen($betfred_show), 3);

which does work, it brings back 888... but it's not really future proof as this just gets the 3 next digits. However, these could change to 4 or 5 digits.

Any suggestions?

answered question

What is the source of your data? It vaguely resembles JSON, but not quite.

@TimBiegeleisen it is serialised data (native to PHP). In this case it is a serialised array

This looks like a php serialized array... Doesn't it make much more sense to deserialize it and use the actual array elements instead of performing string operations on the serialized binary data?

1 Answer

9

It is a serialised array. You can simply unserialize it and access the key

$array = unserialize($input);
$output = $array['betfair_affiliate_id'];

posted this

Have an answer?

JD

Please login first before posting an answer.