我如何在字符串行中输出我的相关错误

问题描述 投票:3回答:2

如果我有像$text = '26061235+1234567,A1227011';这样的字符串行,我想输出一个短于8个字符或包含非数字字符的字符串。

我的预期输出应该是1234567A1227011

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";
        }
   }

但我得不到输出结果

php string
2个回答
1
投票
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;
}

0
投票
$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不行

© www.soinside.com 2019 - 2024. All rights reserved.