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.
Related
This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 2 years ago.
I have a value like 12,25,246
I want to get 1225246 (must be an integer)
How can I do that?
I have searched a lot but there are direct answer like converting string to integer but not like this actually these both are like integer
I have tried php formate_number but it did not worked.
You could use a combination of intval() and str_replace() to do this.
Example:
$value = '12,25,246';
var_dump(intval(str_replace(',','',$value)));
// Yields: "int(1225246)"
Sandbox
$number = (int)str_replace(',', '', '12,25,246');
here is it
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]);
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'];
This question already has answers here:
How to check how many times something occurs in a string in PHP
(5 answers)
Extract a single (unsigned) integer from a string
(23 answers)
Closed 4 years ago.
I am trying to count how many times number appears in a string. For example, in the string "rt_876_io_542_po_367" there are three numbers and in the string "tr_766_ 756" there are two numbers. How do I do this with PHP?? I tried the following code:
$str="RT_657_YT_89";
$key=preg_match_all('!/_[0-9]_/!',$str);
echo $key;
but it echos "0"!! please help
This can easily be accomplished with a regular expression.
function countDigits( $str )
{
return preg_match_all( "/[0-9]/", $str );
}
The function will return the amount of times the pattern was found, which in this case is any digit.
This question already has answers here:
How to convert some character into numeric in php?
(6 answers)
Closed 9 years ago.
Without using a loop, is there a way (function) to convert, for example, the letter "c" to its numeric sequence in the alphabet (3)?
I'm trying to take a literal string - $var = "c" and apply a function to it that returns 3.
Are there any built-in PHP functions that do this? I can't find any online and would rather avoid writing the function if necessary.
Anyone know of such a conversion function?
Yup.
$alphabet_position = ord(strtoupper($character)) - ord('A');