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

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 :: foreach in laravel 
Php :: what is the difference between static and dynamic websites? 
Php :: str_replace smarty template 
Php :: php get list of filenames in diretory 
Php :: how to get correct file or content mime type using/in php 
Php :: php get file location 
Php :: laravel order by numbers 
Php :: laravel eloquent soft delete 
Php :: codeigniter order_by 
Php :: enum artisan codwe 
Php :: eloquent all only one culomn 
Php :: laravel web php request to redirect to another page 
Php :: laravel retry specific failed job 
Php :: woocommerce remove payment method when totla is 0 
Php :: php array to object 
Php :: drupal 8 get enabled languages 
Php :: laravel without global scopes 
Php :: string match in php 
Php :: php PDO database crud class 
Php :: upload multiple files in codeigniter 
Php :: laravel controller in details 
Php :: pagination php mysql 
Php :: php json decode not working on array 
Php :: thousand seperator php 
Php :: laravel controller create command in a folder 
Php :: reset password symfony 
Php :: fast excel export laravel 
Php :: Laravel Model Create Artisan Commant 
Php :: laravel override factory values in database seeder 
Php :: php number to words 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =