Checking input for a number.

Discussion in 'PHP' started by jonathandey, Apr 5, 2010.

  1. #1
    The script below puts the input text into an array, it will then check each individual character for a numeric value. If it finds one it will let you know, other wise it will tell you everything is fine.

    I'm not sure if there is a more efficient way of doing this, but if you just use
    is_numeric($value)
    PHP:
    and the value is h3llo it will will output telling you it is fine.

    
    <?php
    
    $str = "H3llo Friend";
    $max = strlen($str);
    $arr1 = str_split($str);
    
    $number = 0;
    	
    	foreach ($arr1 as $value){
    	if (is_numeric($value))
    	{
    		echo "Number found!";
    		exit();
    		
    	}
    	else
    	{
    		$number++;
    	}
    
    	}
    	
    	if ($number == $max){
    		echo "<p>All fine!</p>";
    	}
    
    
    ?>
    
    PHP:

    Please let me know if there is an easier more efficient way of doing this thanks.
     
    jonathandey, Apr 5, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    if(preg_match('\d', $str)) {
        echo 'String contains a number';
    }else{
        echo 'No number found';
    }
    PHP:
    Something like that?
     
    JAY6390, Apr 5, 2010 IP
  3. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    
    Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in /home/n33t/www/www/numeric.php on line 5
    No number found
    Code (markup):
    That didn't seem to work.
     
    jonathandey, Apr 5, 2010 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oops, I've only just woke up lol. Use this
    if(preg_match('/\d/', $str)) {
     
    JAY6390, Apr 5, 2010 IP
  5. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Well Good Morning! :p

    Thanks for the help that worked perfectly!
     
    jonathandey, Apr 5, 2010 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Morning! No problem :D
     
    JAY6390, Apr 5, 2010 IP
  7. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    Just looking at your mysql_real_escape_string and sprintf function.
    Does this prevent SQL Injection?

    
    function mressf()
    {
        $args = func_get_args();
        if (count($args) < 2)
            return false;
        $query = array_shift($args);
        $args = array_map('mysql_real_escape_string', $args);
        array_unshift($args, $query);
        $query = call_user_func_array('sprintf', $args);
        return $query;
    }
    
    PHP:
     
    jonathandey, Apr 5, 2010 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yeah it will do
     
    JAY6390, Apr 5, 2010 IP
  9. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #9
    Very nice indeed :D
     
    jonathandey, Apr 5, 2010 IP