No integer (0 to 9) is allowed in a string

Discussion in 'PHP' started by chxxangie, Nov 9, 2008.

  1. #1
    how to do identify a string inside has no integer (0 - 9)?
    when i try to write source code for this, it make me blur... :eek:
    please help... thanks...
     
    chxxangie, Nov 9, 2008 IP
  2. garrettheel

    garrettheel Peon

    Messages:
    341
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You'll need to use a regular expression, do a google search for one
     
    garrettheel, Nov 9, 2008 IP
  3. chxxangie

    chxxangie Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
    
    function to_echo($f_input1,$f_input2,$debug){
    	if($debug == 1){
    		echo $f_input1. " => ".$f_input2."<br>";
    	}	
    }
    
    $name = "lu chen";
    to_echo("name",$name,1);
    
    $name1 = str_replace(" ","",$name);
    to_echo("name",$name1,1);
    
    $len_name1 = strlen($name1);
    to_echo("length name",$len_name1,1);
    
    for($a=1;$a<$len_name1+1;$a++){
    	$char_name = substr($name1,$a-1,1);
    	echo "str: ".$char_name."<br>";
    	if(is_numeric($char_name)){
    		echo "contain integer!"."<br>";
    		
    	}
    }
    
    ?>
    
    PHP:
    finally has done... however i had written a long code just for validation of name :eek:
     
    chxxangie, Nov 9, 2008 IP
  4. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #4
    If you only want to detect, not sanitize it and remove it, you can use this.
    
    $input = 'asda091283xxx';
    
    if (preg_match('/([0-9]+)/', $input)) {
       echo 'You are a malicious user. Do not put numbers here.';
    } else {
       echo 'You are a nice user. Thank you.';
    }
    
    PHP:
    But if you want to remove it automatically, use this.
    <?php
    $input = 'asda091283xxx';
    $clean_input = preg_replace('/([^0-9]+)/', '', $input);
    echo 'New and clean one : ', $clean_input;
    ?>
    PHP:
     
    xrvel, Nov 9, 2008 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    joebert, Nov 10, 2008 IP