checking a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

checking a string

Post by hmsg »

Hi.

I have a string, but i want to check if the chars of the string are all valid, because the string can't contain certain chars, so is there any way that i define the chars that the string is possible to contain and then compare with the string and get false or true if th string is ok or if she got wrong chars?


With the best regards

Hugo Gomes
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Check out preg_match()
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

I can't use preg_match() ;)

Code: Select all

function checkCharacters($text)
{
	$allowed = "1=2=3=4=5=6=7=8=9=0=q=w=e=r=t=y=u=i=o=p=a=s=d=f=g=h=j=k=l=z=x=c=v=b=n=m=.=(=)= =,=-=_=;=:=?=!";
	$arr = explode("=", $allowed);
	sort ($arr);
	for ($i = 0; $i <= strlen($text); $i++) 
	{
		$letter = strtolower(substr($text, $i, 1));
		if (!in_array($letter, $arr))
		{
			return false;
		}
	} 
	return true;
}
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

can't or won't? It's core....
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Grim's still on php/fi -- really, Grim, it's time to upgrade :D
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

preg would probably be faster after all the str_replace iterations are tallied
Post Reply