skip to Main Content

i have a textarea each line in textarea contains numbers or names , i want to extract only number from textarea .

this my code


<?php 

    $allUsers = $_POST['allusers'];
    foreach(explode("n", $allUsers) as $line) {
        
        if (is_numeric($line)) {
            echo $line."n";
        }

    }
?>

and example of textarea data :

<textarea>
156444
978455
amoka
123
auman
</textarea>

2

Answers


  1. Remove whitespace around the line before checking if it’s numeric.

        foreach(explode("n", $allUsers) as $line) {
            $line = trim($line);
            if (is_numeric($line)) {
                echo $line."n";
            }
        }
    
    Login or Signup to reply.
  2. A solution using regex

     $text = 'ggd 56756 sadhgsagdahdgash  dhjghjg 324324 3  432 423 4 324hjhghjgjh 343434 34 34 hgjhghj';
        
        preg_match_all('/d+/', $text, $matches);
        
        $numbers = $matches[0];
        
        print_r($numbers);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search