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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chxxangie
Forum Newbie
Posts: 12
Joined: Wed Sep 19, 2007 10:00 pm

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

Post by chxxangie »

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...
please help... thanks...
chxxangie
Forum Newbie
Posts: 12
Joined: Wed Sep 19, 2007 10:00 pm

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

Post by chxxangie »

finally has done. but the length code is a bit long... :oops:

Code: Select all

 
<?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>";
        
    }
}
?>
 
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

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

Post by Syntac »

Code: Select all

if( preg_match( "#[0-9]#", $string ) )
    echo "String contains numbers.";
else
{
    // do stuff
}
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

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

Post by Mark Baker »

Code: Select all

 
if (strtok($testString,'0123456789') != $testString) {
    echo 'String contains numeric values';
} else {
    echo 'String contains no numeric values';
}
 
Post Reply