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!"; 
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.