如何从数组中提取数字值,然后将其与另一个字符串组合


How to extract number values from an array and then combine it with another string?

我有一个数组 $qwe 2,我需要从中制作 2 个单独的数组。一个将不包含数字值在本例中妈妈,姐姐和一个具有数值的数组 11爸爸 13兄弟。

$qwe = " mom   11dad  sister  13brother ";
$qwe0 = ucwords(strtolower($qwe));
$qwe1 = preg_replace('/'s+/', ' ',$qwe);
$qwe7 = trim($qwe1);
$qwe2 = explode(' ',$qwe7);
var_dump($qwe2);

这是它的样子:

array (size=4)
  0 => string 'mom' (length=3)
  1 => string '11dad' (length=5)
  2 => string 'sister' (length=6)
  3 => string '13brother' (length=9)

上面的所有这些东西也是需要的,但我设法轻松地做到了。我不明白下面的部分。

期望结果 : $asd = array("mom, sister");$zxc = array("11dad, 13brother");

此外,我还有一个字符串$doyou = "Do you like ?"我需要将其与新数组$asd组合,这将导致: Do you like mom?, Do you like sister?

提前感谢!

将 PHP 的array_filter()与一些检查字符串中的数字的自定义函数一起使用:

$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);
}

然后array_map()可以帮助您替换字符串:

echo implode(', ', array_map('myStringReplace', $asd));
function myStringReplace($string)
{
    return str_replace('?', $string, 'Do you like ?');
}
相关文章: