Find string, then find string in-between chars [duplicate] - php

This question already has answers here:
What kind of data format is that?
(2 answers)
Closed 4 years ago.
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?

It is a serialised array. You can simply unserialize it and access the key
$array = unserialize($input);
$output = $array['betfair_affiliate_id'];

Related

How to split this type of string and get particular values in PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I Have a string like this
{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":36,"Code":891,"Message":"Success"}-
I need the value of Balance.
I tried like this.
$string = '{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":1,"Code":1,"Message":"Success"}-';
$withCharacter = strstr($string, 'Balance":');
echo substr($withCharacter, 1);
Tried to use explode also but no luck.
This seems like a valid JSON, why not json_decode and find the value:
$i = json_decode('{"Account":{"Currency":"SGD","CreditLimit":0.0000,"Balance":1649.5700},"Status":36,"Code":891,"Message":"Success"}');
echo $i->Account->Balance;

How to put specific string in into array? [duplicate]

This question already has answers here:
PHP: Best way to extract text within parenthesis?
(8 answers)
Closed 3 years ago.
I have a string like this:
[Rise] and rise [again] until lambs become [lions].
I want to get all the string inside the [] (Rise, again, lions) and put it in to an array. How can I do this?
Edit: Thank you guys, I already found the solution here: PHP: Best way to extract text within parenthesis?
You can do it with regular expressions:
$s = '[Rise] and rise [again] until lambs become [lions].';
preg_match_all('/\[([^\]]+)\]/', $s, $m);
print_r($m[1]);

How would I take a string, find a certain character, add a character, and continue until end of string? [duplicate]

This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you

Need Help on Filtering the json to grab only the values [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I want to apply the regex to extract only the values. I am not getting the perfect one any help
[{"name":"basket ball"},{"name":"foot ball"},{"name":"sports"}]
There's absolutely no need for a regex here. Use json_decode():
$string = '[{"name":"basket ball"},{"name":"foot ball"},{"name":"sports"}]';
$data = json_decode($string, true);
now you have a normal php array $data to get your wanted data from.
like
echo $data[0]['name']; // basket ball

Using PHP to find the number inside {{2461245}} [duplicate]

This question already has answers here:
PHP Extract numbers from a string
(8 answers)
Closed 7 years ago.
What is the best way to extract the number in the following string:
"This is a test string {2461245} and I need to extract the number"
Thank you.
This should help you :
$myText = 'This is a test string {2461245} and I need to extract the number';
preg_replace("/[^0-9]/","",$myText);
What you are saying there is : I want to replace everything which is not a number in the myText variable by "", which means everything which will stay in the end is your number.

Categories