Forum Moderators: coopster

Message Too Old, No Replies

php character count & accepted character

         

patrixs

10:58 pm on Mar 29, 2003 (gmt 0)

10+ Year Member



Hey,
I'm making a php script and I'm not sure what function lets you count the number of characters in a string and the function that accepts only certain characters (I think it's called a pattern), If the characters arn't good it returns false and if they mach it return true. Could you help me please?

Thanx,
If any problem ask them!
Patrick

DrDoc

11:37 pm on Mar 29, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



int strlen(string);

echo strlen("blah");

Prints 4


Pattern matching is accomplished through regular expressions. RegExp can be quite complicated. (preg_replace documentation [php.net])

For example, if you want to match only letters and numbers, you can do something like this:

if(preg_match("/[A-Za-z0-9]/",$string)) {
echo "The string '$string' contains only letters and numbers";
}

If you want to match anything but letters and numbers you do this:

if(preg_match("/[^A-Za-z0-9]/",$string)) {
echo "The string contains some other character than letter and number";
}

PHP Pattern Syntax [php.net]
Perl Regular Expressions [perldoc.com] - good start for regexp newbies

patrixs

4:09 am on Mar 30, 2003 (gmt 0)

10+ Year Member



Ok kool, thanx a bunch! :D

patrixs

9:18 pm on Mar 30, 2003 (gmt 0)

10+ Year Member



Ok, I've tryed it but it only returns false if the string is like "#######" and it returns true if it's "fdsdfs#f" is there a way to return true even if there is only one wrong character?

Or is that just me? Can someoen else try and let me know please?

Thanx