Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Extract Numbers From a String in PHP

phpCopy<?php 
$string = 'Sarah has 4 dolls and 6 bunnies.';
$outputString = preg_replace('/[^0-9]/', '', $string);  
echo("The extracted numbers are: $outputString 
"); 
?> 
Comment

php get only numbers from string

$str = 'In My Cart : 11 items';
$int = (int) filter_var($str, FILTER_SANITIZE_NUMBER_INT);
Comment

php get number from string

function get_numerics ($str) {
    preg_match_all('/d+/', $str, $matches);
    return $matches[0];
}

// this function will return an array of number

$str = "3 dogs were running!";
echo (get_numerics($str)[0]); 

// output 3
Comment

Extract Numbers From a String in PHP

phpCopy<?php 
$string = 'Sarah has 4 dolls and 6 bunnies.';
$int = (int) filter_var($string, FILTER_SANITIZE_NUMBER_INT);  
echo("The extracted numbers are: $int 
"); 
?> 
Comment

PHP Numeric String

/*
The PHP is_numeric() function can be used to find whether a variable is 
numeric. The function returns true if the variable is a number or a 
numeric string, false otherwise.
*/

#How to check if the variable is numeric

<?php
$x = 24523;
var_dump(is_numeric($x));

$x = "24523";
var_dump(is_numeric($x));

$x = "124.14" + 100;
var_dump(is_numeric($x));

$x = "Random Text";
var_dump(is_numeric($x));
?>
  
/*This outputs 
bool(true)
bool(true)
bool(true)
bool(false)
*/
Comment

Extract Numbers From a String in PHP

phpCopy<?php 
$string = 'Sarah has 4 dolls and 6 bunnies.';
preg_match_all('!d+!', $string, $matches);
print_r($matches); 
?> 
Comment

PREVIOUS NEXT
Code Example
Php :: laravel automatically generate unique username 
Php :: wordpress get post featured image 
Php :: how unique field in table in phpmyadmin 
Php :: php day of week full name 
Php :: laravel validation array input 
Php :: php get this week date range 
Php :: php query pdo 
Php :: laravel has one 
Php :: how to get attribute value in xml using php 
Php :: Create Model with Controller in Laravel 
Php :: get age in months php 
Php :: wordpress log errors 
Php :: laravel wherenotin 
Php :: how to pass parameter in routes of laravel 
Php :: how to remove annoying plugin notification in wordpress 
Php :: display image from database in laravel 
Php :: PHP strstr — Find the first occurrence of a string 
Php :: laravel 5 use env variable in blade 
Php :: laravel create request 
Php :: get term id by post id 
Php :: php set environment variable 
Php :: SPA and keep some of the Laravel pages you need to have a route like this 
Php :: read line by line php 
Php :: give custom field name in laravel form validation error message 
Php :: time function in php 
Php :: Error: Cookies are blocked or not supported by your browser. You must enable cookies to use WordPress. 
Php :: array reverse php 
Php :: convert std to array php 
Php :: laravel amount migration 
Php :: laravel where not equal 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =