how to remove special characters from url
Moderator: General Moderators
how to remove special characters from url
Hello,
I am new to php and I really dont know how to get this done.
I am making a members site. Each users will given a unique url, some thing like this http://<user name>.domain.com . I dont want to let user have url like this http://<user name>.<user name 2>.domain.com . That is basically, I want to remove characters such as ".", ",", "/", "\", "!" .. and so on.
I did make some basic searches but I have no idea, how to go about this. I will really appreciate is some one shows me a tutorial or some thing, that would help me.
Thank you,
I am new to php and I really dont know how to get this done.
I am making a members site. Each users will given a unique url, some thing like this http://<user name>.domain.com . I dont want to let user have url like this http://<user name>.<user name 2>.domain.com . That is basically, I want to remove characters such as ".", ",", "/", "\", "!" .. and so on.
I did make some basic searches but I have no idea, how to go about this. I will really appreciate is some one shows me a tutorial or some thing, that would help me.
Thank you,
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: how to remove special characters from url
Code: Select all
$invalidCharacters = array('.', ',');
if (strpos($username, $invalidCharacters) !== FALSE) {
// Invalid username
}Re: how to remove special characters from url
If you want to replace everything that isn't alphanumeric or a dash (which I think is what valid domain/subdomains can consist of), you could try this:
EDIT
superdezign, just saw your reply...yeah, you could do that as well. Depends if you want to replace invalid characters, or notify your user that there are invalid characters.
Code: Select all
$str = preg_replace('/[^A-Za-z0-9\-]/', null, $str);EDIT
superdezign, just saw your reply...yeah, you could do that as well. Depends if you want to replace invalid characters, or notify your user that there are invalid characters.
Re: how to remove special characters from url
You could also just pick the characters you want to remove
This removes the characters
http://cnn.com!and,this\junk
to this:
httpcnncomandthisjunk
If you want to replace them with spacesChanges
http://cnn.com!and,this\junk
to this:
http cnn com and this junk
Code: Select all
$input=preg_replace("/[:.,!\/\\\\]/i","",$input);http://cnn.com!and,this\junk
to this:
httpcnncomandthisjunk
If you want to replace them with spaces
Code: Select all
$input=preg_replace("/[:.,!\/\\\\]/i"," ",$input);http://cnn.com!and,this\junk
to this:
http cnn com and this junk
Re: how to remove special characters from url
in naive way...

Code: Select all
<?php
$url="http://amit.raju.asit.domain.com";
$part = str_ireplace("http://","",$url);
$part = str_ireplace(".domain.com","",$part);
$spc_arr = array(".", ",", "/", "!" );
for($i=0; $i<count(spc_arr); $i++) {
$part=str_ireplace($spc_arr[$i], "", $part);
}
$url = "http://" . $part . ".domain.com";
echo ($url);
?>Re: how to remove special characters from url
I prefer my example
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: how to remove special characters from url
preg_replace() is cleaner, but str_replace() is faster.jackpf wrote:![]()
I prefer my exampleNo offense.
Re: how to remove special characters from url
True...but that for() loop probably slows stuff down though.
@lipun4u
You do know str_replace() takes arrays as args right?
@lipun4u
You do know str_replace() takes arrays as args right?