Page 1 of 1

checking a string

Posted: Tue Jan 23, 2007 6:35 am
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

Posted: Tue Jan 23, 2007 6:38 am
by aaronhall
Check out preg_match()

Posted: Tue Jan 23, 2007 6:39 am
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;
}

Posted: Tue Jan 23, 2007 12:55 pm
by Kieran Huggins
can't or won't? It's core....

Posted: Tue Jan 23, 2007 1:05 pm
by aaronhall
Grim's still on php/fi -- really, Grim, it's time to upgrade :D

Posted: Tue Jan 23, 2007 1:11 pm
by wtf

Posted: Tue Jan 23, 2007 1:13 pm
by aaronhall
preg would probably be faster after all the str_replace iterations are tallied