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

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]);

Related

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

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'];

REGEX with PHP (what am I doing wrong?) [duplicate]

This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 3 years ago.
Let's say I want to place the bold content into a variable called $question_text.
1} What color is the sky:
A) Answer A
B) Answer B
C) Answer C
Answer: A
How do I use preg_match to do that?
I tried:
if(preg_match("#}(.*)#", $question, $question_match))
{
//Extract the question text
$question_text = trim($question_match[1]);
But it only gives me the first line: **What color is the sky: **
What am I doing wrong?
#(?s)}(.*)+?Answer: [A-Z]#
Worked for me!

PHP Replace text in string [duplicate]

This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 6 years ago.
i have a problem.
I want in php to replace all 'blablabla' with 'bla'(because i hate blablabla). Here's my code:
<?php
$string = 'Dracula always says BlaBlaBla but says he never says BlaBlaBla';
$result = answer to replace here
?>
Thanks
Use srt_replace function to do that like this :
str_replace("BlaBlaBla","bla",$string);
You can use something like this:
str_replace("BlaBlaBla","bla",$string)
Also you can find it here in the docs: http://php.net/manual/en/function.str-replace.php

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.

How to find 3 or more of a character in a string? [duplicate]

This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Closed 9 years ago.
$string = "oidjdssd , odi,jdois, 3089u,, oisdjsd";
How do i find out if theres more than 3 commas in the string above in the best way?
I would suggest substr_count. You can see if the result is >3 to see if there's more than three.
echo count_chars($string)[ord(',')];
Or for PHP<5.4
$chars = count_chars($string);
echo $chars[ord(',')];
BTW: As it seems, that you are handling CSV-data, you should have a look at str_getcsv()

Categories