如何将包含数字和字符的数组划分为两个数组并打印


how to divide array which contains numeric and characters into two arrays and print them?

这里我的问题是如何划分一个同时包含数字和字符的数组。例如:125毫克。我从数据库中得到这个值,我试图分割它,但我无法分割它。有人能给我建议吗?我在谷歌上搜索了很多。

这是代码示例:

<?php 
$sql="select * from medication_history where id=$id";
$result=mysql_query($sql)or die(mysql_error());
$row=mysql_fetch_array($result);
$dose=$row['dose'];
//echo $dose;
echo str_word_count($dose);
$dose=implode('',explode(',',$dose));
//print_r ($dose);
 echo $dose;?>

我认为最合适的解决方案是将"剂量"列拆分为两个不同的列。第一个含有量(125),第二个含有单位(mg)。

我看到的另一种方式是使用regex来识别数字部分和字母字符部分。

如果理解您的问题。答案是:

$num=preg_replace("#[^0-9]#",'', $dose);
$char=preg_replace("#[^a-z]#",'', $dose);

现在:

$num=125和$char=mg。