Page 1 of 1

check if string contains specific characters

Posted: Sat Dec 03, 2011 9:49 pm
by Jackount
I have this function in a log script:

Code: Select all

	function ctype($l) { //Returns TRUE if link only contains valid signs.
		$nl = $l; //yes, this is "L"'s
		$keys = array("æ","ø","å","Æ","Ø","Å","å","ø","Æ","æ","Ø","Å","@", "£", "¤", "%", "&", "/", "\\", "^", "*", "§", "½", ".", ",", ":", ";", "'", "¨", "~", "[", "]", "{", "}", "´", "`", "@", "!", '"', "'"," ");
		$array = array_fill_keys($keys, "-");
		$nl = strtr($l,$array);
		if ($nl != $l)
			return false;
		else return true;
	}
It does what it's told to do, perfectly, BUT the æøå,ÆØÅ,å etc. chars. Those it just ignores :o
Otherwise it works perfectly fine.
What is it about the æøå signs? As you can see, I've tried in some different ways.

Re: check if string contains specific characters

Posted: Sun Dec 04, 2011 6:05 am
by maxx99
Why wouldn't you use

Code: Select all

/*** a URL ***/
$url = "http://www.phÆpro.oФrg";

/*** sanitize the URL ***/
echo filter_var($url,  FILTER_SANITIZE_URL);
http://php.net/manual/en/filter.filters.sanitize.php

Re: check if string contains specific characters

Posted: Sun Dec 04, 2011 9:12 am
by Jackount
Well, the problem is, that the value is not a link...
it's a sublink (http://awebsite.com/***THIS***)

Should I just add http://awebsite.com/, then sanitize, then remove http://awebsite.com/ again?

EDIT: And it cannot have &,/, etc, so sanitize_url won't work, am I right?

Re: check if string contains specific characters

Posted: Sun Dec 04, 2011 11:16 am
by maxx99
Well only thing this function does is:
Remove all characters except letters, digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=.

If you want to check if link or sublink provided by someone is ok just compare strings before and after sanitize :) there is no need to add http://..../