Jump to content

Only Allowing Alpha Numeric


livethedead

Recommended Posts

Sorry that was unclear, I meant how to check if anything other than a-z 0-9 is in the string. I figured it out though incase any other newbie runs across the same question. If there's a better way I'm all for improving.

 

<?php
$regex = preg_match_all("%[a-zA-Z0-9]%", $str, $array);

if (strlen($str) > $regex) {
echo "only alpha or numeric combinations are allowed";
} else {
	echo "success!"; 
}
?>

Generally, if you can accomplish a task with simple string functions, then don't bother with a regex pattern.

 

yes "livethedead" I agree with pika. While I do love regex, if there is a simple PHP functions solution to the problem, I would always go with that. Especially with large strings such as text files etc. They tend to be much more efficient than using a regular expression in most cases. It is only in special cases where the solution cannot be done with simple PHP functions or it would be way too much of an effort to do so where a regex should be used.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.