Portal Home > Knowledgebase > Articles Database > Removing Character & Spaces from Links


Removing Character & Spaces from Links




Posted by 1boss1, 08-08-2008, 08:06 PM
Hello, I'm not much of a coder, but i'm completely stuck on this one. I have a database with poems and it does work fine, but the URL's and Links are pretty bad and i'm trying to clean them up. I'm using the poem title for the URL, however poems with symbols or spaces in the title end up being in the URL. For example: The code: The .htaccess: A poem Title and URL will end up like the following.. Any ideas on how i can drop or ignore symbols and just have alpha numeric values, as well as convert spaces in to dashes? So the desired URL would become: Having everything lower case would be a bonus, but just getting rid of spaces and characters would be excellent. As you can imagine, having a dozen %20 and characters in URL's is pretty bad. Thanks in advance of any help.

Posted by Barti1987, 08-08-2008, 10:39 PM
Use str_replace function. Peace,

Posted by etogre, 08-09-2008, 12:38 AM

Posted by 1boss1, 08-09-2008, 01:54 AM
Azizny, thanks for putting me on to the str_replace function it gave me something to Google. Through a stroke of luck, i came up with And behold it worked. But the drawback was it removed spaces in places that weren't a URL also. So the plain text titles on the page came out. This-is-my-Title etogre, wow that looks pretty sophisticated. I may need an hour or three to digest that one and work out how/where to use it. Thank you very much for the code, i'll do some testing and see how it goes.

Posted by etogre, 08-09-2008, 02:01 AM
It's really not that excessive; sanitizing all input is a very important aspect of any application. Which includes characters like this: ŠěžťčýšúěᾡᾧῘઍિ૮

Posted by 1boss1, 08-09-2008, 02:50 AM
Oh no doubt it's not excessive, it looks like it's the perfect solution. What i meant by sophisticated is it's quite technical to me, kind of like a chimp doing brain surgery. And yes i do have some odd characters like you posted above, so being able to get rid of them will be excellent.

Posted by 1boss1, 08-09-2008, 03:55 AM
Nope, this isn't working. 14 hours trying to get a URL to go from /file%20name.html to /file-name.html is a bit much, so i'm willing to settle for anything. Currently this does work: But it also adds a - to plain text, link anchor text. Is there any way of making the above code effect only URL's and not text?

Posted by vibrokatana, 08-09-2008, 04:06 AM
A lot of CMS use an "anchor" column for use in URLs. You may want to consider making a new column and storing the generated one there (with an index if your using it as a look up)

Posted by xenex, 08-09-2008, 04:40 AM
Try googling 'str_replace' I think that should help you out, basically you would then replace ' ' with '' (space with nothing) cheers sean

Posted by etogre, 08-09-2008, 01:35 PM
Indeed this is the way to do it. So for instance in your poem database: you'd take the title of the poem, put it through the cleanURL() function, and then store it in a new column called 'url' or whatever. So you'd end up doing something like this

Posted by 1boss1, 08-09-2008, 06:16 PM
Hmm ok, thanks for your help everyone but this is obviously way over my head. I have tried all the codes above and they either do nothing or cause errors. I thought making a URL go from /file%20name.html to /file-name.html would be a matter of changing a couple of characters of php or maybe a line at the most and take 5 minutes tops. But after 2 fulls days of doing nothing but work on this it's obviously never going to work. I will just leave the spaces and characters in the URL's. Thanks everyone.

Posted by localhost127, 08-11-2008, 12:55 AM
I might be missing something here, but why not just use urlencode and htmlentities? urlencode will make the URL safe, and htmlentities will make the title display right in the browser (assuming special characters)

Posted by Doh004, 08-11-2008, 08:41 AM
For SEO reasons it's best not to have spaces or odd characters like a & in your URL. The "cleaner" the url, often the better results you get from search engines.

Posted by localhost127, 08-11-2008, 11:27 AM
Modern search engines shouldn't have problems with odd characters. An extremely large percentage of dynamic sites exist and search engines typically have no problems indexing them. Using mod_rewrite to clean up the URL's is fine, but not too necessary today for SEO. If quotes and random characters are such a problem in the titles, then the title should not be used for filenames/links. The filename is not important, the content is.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Burst Down? (Views: 657)
Exploit Scanner s (Views: 608)