如果我有像$text = '26061235+1234567,A1227011';
这样的字符串行,我想输出一个短于8个字符或包含非数字字符的字符串。
我的预期输出应该是1234567
和A1227011
。
1234567
,因为它长7个字符。 A1227011
因为它包含A
。
这是我写的代码。
$text = '26061235+1234567,A1227011';
$splitted = preg_split('/[(or),\+]/', $text);
$splitted = array_filter($splitted); // remove any empty string
foreach($splitted as $str)
{
if(!is_numeric($str) || strlen($str)<=8)
{
$error=preg_replace('/\d+/','',$str);
echo "this $error is not fine";
}
}
但我得不到输出结果
if(!is_numeric($str) || strlen($str)<=8)
{
$error=preg_replace('/\d+/','',$de);
echo "this $error is not fine";
}
你为什么需要preg_replace('/\d+/','',$de);
?为什么不呢:
if(!is_numeric($str) || strlen($str)<=8)
{
echo $str;
}
$text = '26061235+1234567,A1227011';
$splitted = preg_split('/[(or),+]/', $text);
$splitted = array_filter($splitted); // remove any empty string
foreach($splitted as $str)
{
if(!is_numeric($str) || strlen($str)<8)
{
$error=preg_replace('/d+/','',$str);
echo "this $error is not fine<br/>";
}
}
输出: -
这1234567不好
这个A1227011不行