solink
solink

Reputation: 45

how do i output my related error in the string line

If I have a string line like $text = '26061235+1234567,A1227011';, I want to output the a string that is either shorter than 8 characters or contains non-numeric characters.

My expected output should be 1234567 and A1227011.

1234567 because it is 7 characters long. A1227011 because it contains A.

This is the code I have written.

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

But i get no result in output

Upvotes: 3

Views: 49

Answers (2)

Rajeev Ranjan
Rajeev Ranjan

Reputation: 4142

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

Output :-

this 1234567 is not fine

this A1227011 is not fine

Upvotes: 0

Sergi Pasoevi
Sergi Pasoevi

Reputation: 2851

if(!is_numeric($str) || strlen($str)<=8)
{
      $error=preg_replace('/\d+/','',$de);
      echo "this $error is not fine";
}

Why do you need preg_replace('/\d+/','',$de); here? Why not just:

if(!is_numeric($str) || strlen($str)<=8)
{
      echo $str;
}

Upvotes: 1

Related Questions