php - How to extract number values from an array and then combine it with another string? -
i have array $qwe2 need make 2 separate arrays. 1 contain not numeric values in example mom, sister , array numeric values 11dad 13brother.
$qwe = " mom 11dad sister 13brother "; $qwe0 = ucwords(strtolower($qwe)); $qwe1 = preg_replace('/\s+/', ' ',$qwe); $qwe7 = trim($qwe1); $qwe2 = explode(' ',$qwe7); var_dump($qwe2); this looks :
array (size=4) 0 => string 'mom' (length=3) 1 => string '11dad' (length=5) 2 => string 'sister' (length=6) 3 => string '13brother' (length=9) all these things above needed managed them easily. don't understand part below.
desired result : $asd = array("mom, sister"); , $zxc = array("11dad, 13brother");
also have string $doyou = "do ?" need combine new array $asd result in : do mom?, sister?
thanks in advance!
use php's array_filter() custom functions check numbers in string:
$asd = array_filter($qwe2, 'hasnumbers'); $zxc = array_filter($qwe2, 'hasnonumbers'); function hasnumbers($string) { return strcspn($string, '0123456789') != strlen($string); } function hasnonumbers($string) { return strcspn($string, '0123456789') == strlen($string); } then array_map() can string replace:
echo implode(', ', array_map('mystringreplace', $asd)); function mystringreplace($string) { return str_replace('?', $string, 'do ?'); }
Comments
Post a Comment