jcadima
2/2/2017 - 10:30 PM

Extract digits from string

Extract digits from string

<?php

/*
extract numbers of a given string and sum these numbers
"3.14 is pie"     is considered  3 + 14 = 17  not 3.14 
"abcd*8 and 2a"   will be 8 + 2 = 10 
*/

$input = 'bla_bla88bla.ac33titor';
$input = 'In 2017, I want to know how much does iPhone 7+ cost? and samsung *8';
$items = preg_split('/[^\d]/', $input);

print_r($items);

echo array_sum($items) ;