how to return a numeric value from string in php?
Do you want to extract it from a string or the string is a number itself?
# case 1
$str1 = "Buy 12 apples, 1.5kg of beans and 4 onions, it's already 12:35 hurry up, it's l4t3!";
# case 2
$str2 = '12';
In first case by using preg_match_all() with a simple pattern like this /[\d]+/ you get:
 => Array
 => 12
 => 1
 => 5
 => 4
 => 12
 => 35
 => 4
 => 3
it will just scan for digits, it's not searching for floats or dates or time and will include the numbers in the word l4t3 (i.e. late), docs:
In the second case use type hint:
$a = (int) $str2;
$b = intval($str2);
which works for integers. You will need floatval() to get decimals. Docs:
Note: please, next time use an appropriate title.
Edited 2 Years Ago
I have been trying to hide and show css with button click:
grid-template-columns: 350px auto 300px;
grid-template-rows: 800px 800px 800px;
"menu cont sidenav";
How to take part of url and store it into a variable?
/home/pages/id --> I want to take id out and store it into $id.
How to do ...
My script merges 18 files and returns all numbers that occur >=13 times within the merger. I timed my script and array_count_values is so slow it accounts for 80% of ...