
January 23rd, 2009, 09:29 AM
|
 |
Sarcky
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
I ran your code and got the correct results:
PHP Code:
<?php
function check_string($Valstring) {
$ValResult=0;
preg_match_all('/^[a-zA-Z- .,]{2,200}$/', $Valstring, $matches);
foreach($matches[0] as $value){
if($value!='' || $value!='0' || $value!='null'){
$ValResult=1;
}
else{
$ValResult=0;
}
}
return $ValResult;
}
$array = array("hulu", "hulu-", "hulu123", "hulu]", "hulu+");
foreach ( $array AS $Valstring ) {
echo "$Valstring has a result of " . check_string($Valstring) . "\n";
}
?>
However, this is a very inefficient way of doing this. A better function:
PHP Code:
<?php
function check_string($Valstring) {
if ( preg_match('/^[a-zA-Z .,-]{2,200}$/', $Valstring) ) {
return 1;
}
return 0;
}
$array = array("hulu", "hulu-", "hulu123", "hulu]", "hulu+");
foreach ( $array AS $Valstring ) {
echo "$Valstring has a result of " . check_string($Valstring) . "\n";
}
?>
Both of these output:
Code:
hulu has a result of 1
hulu- has a result of 1
hulu123 has a result of 0
hulu] has a result of 0
hulu+ has a result of 0
-Dan
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|