FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


How?:Check the occurance of a certain character in a string.





James007
Hello PHP experts! (well I hope so)

I have a string $a, and this string should only contain the following characters: "a" till "z" (and the capitalized versions), "0" till "9", the underscoar, and the "-" sign.

How do I make a code checking this? I'm thinking about using ereg() or something alike, but those functions are quite incomprehensible for me (a php noob)

Please help me. Sad
SlowWalkere
Well, I'm not sure about the most efficient or fastest way to do that. I would have thought that there'd be a built in function to check if something were a letter/number, but I didn't see one while looking at php.net. So here's one way I came up to do it.

Basically, we want to make up a string of all the possible characters (a-z, 0-9, and dash). Then, go one by one through the original string $a to see if each character matches the set of valid characters.

Code:
$a = "Our string";
$valid = "abcdefghijklmnopqrstuvwxyz0123456789-_";

$strIsValid = 1;  // This will be set to zero if a character is invalid

foreach ($a as $pos => $letter)  // Run through and check each letter
{
  $lowerLetter = strtolower($letter);  // Convert to lower case, so caps are ok
  if (strpos($valid, $lowerLetter) === false)
  {
     $strIsValid = 0;
  }
}

if ($strIsValid)
  echo "String is valid";
else
  echo "String contains an invalid character.";


So the script runs through the entire string, checking each character one at a time. If that character is not found in the reference string, then $strIsValid is flagged and set to 0.

That should basically do what you want, although I haven't tested it.

Good luck,
- Walkere
hexkid
James007 wrote:
I have a string $a, and this string should only contain the following characters: "a" till "z" (and the capitalized versions), "0" till "9", the underscoar, and the "-" sign.

How do I make a code checking this? I'm thinking about using ereg() ...
I prefer preg_match()
Code:
<?php
if (preg_match('/^[a-zA-Z0-9_-]*$/', $a)) {
  ### string is good
} else {
  ### string is not good
}
?>
James007
Thank you both for replying.

A little question though. Could you make me understand what '/^[a-zA-Z0-9_-]*$/' actually means? I see that between [] you've declared all the good characters, but what do the other symbols mean?

Thanks,

James
hexkid
James007 wrote:
Could you make me understand what '/^[a-zA-Z0-9_-]*$/' actually means?
Check the manual entry for regular expressions syntax. But I'll give it a try:

/.../ ==> regular expression delimiters
^ ==> beggining of subject
[] ==> group of characters
a-zA-Z0-9_- ==> characters
* ==> zero or more matches
$ ==> end of subject

So it means "check the entire subject for any number of these characters". I could "check if something other than these characters appear (at least) once anywhere in the subject" with
Code:
$rx = '/[^a-zA-Z0-9_-]/';
The ^ here, because it's the first character inside the [] means "anything but the following characters".
Code:
<?php
if (preg_match('/[^a-zA-Z0-9_-]/', $a)) {
  ### string is not good
} else {
  ### string is good
}
?>
Stubru Freak
hexkid wrote:
Code:
$rx = '/[^a-zA-Z0-9_-]/';


I might be wrong, but shouldn't you escape the -? Like this:
preg_match('/[^a-zA-Z0-9_\-]/', $a)
hexkid
Stubru Freak wrote:
I might be wrong, but shouldn't you escape the -? Like this:
preg_match('/[^a-zA-Z0-9_\-]/', $a)


Actually it isn't needed for this example.
The '-' between the square parenthesis specifies a range ... unless it's escaped or it is the last character in the "group"

Code:
$rx = '/[a-e]/'; // a, b, c, d, and e, in most (all????) encodings
$rx = '/[a\-e]/'; // a, minus, and e
$rx = '/[a-e-]/'; // a, b, c, d, e, and minus
$rx = '/[a-e-h]/'; // a, b, c, d, e, minus and h
                   // BUT I'D NEVER WRITE IT LIKE THIS!
Stubru Freak
hexkid wrote:
The '-' between the square parenthesis specifies a range ... unless it's escaped or it is the last character in the "group"


Thanks, I didn't know this.
Related topics
FIRE FOX CUTOMIZATIONS
Fortran Tutorials(77)
C++ int to char and char to int
md5 encryption not reliable.
Wrong order
There is a game called Xiah
PHP Game..
Get specific parts of a string
hashing your passwords
Will Google present newer pages on top of older ones ?
Clean of guys
SCAM E-MAILS
pitbull
URL Masking with .TK
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.