check if string contains specific characters

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
Jackount
Forum Newbie
Posts: 13
Joined: Wed Aug 10, 2011 5:38 am

check if string contains specific characters

Post 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.
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: check if string contains specific characters

Post 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
Jackount
Forum Newbie
Posts: 13
Joined: Wed Aug 10, 2011 5:38 am

Re: check if string contains specific characters

Post 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?
maxx99
Forum Contributor
Posts: 142
Joined: Mon Nov 21, 2011 3:40 am

Re: check if string contains specific characters

Post 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://..../
Post Reply