Author Topic: Dealing with ' & : in url  (Read 464 times)

0 Members and 1 Guest are viewing this topic.

Offline graham23sTopic starter

  • Devotee
  • Posts: 883
    • View Profile
Dealing with ' & : in url
« on: November 22, 2009, 10:19:35 AM »
Hi Guys,

I have written a shopping cart that uses the Amazon API, a lot of amazon's products bring back some funky titles for example: The Major Works: including The Rape of the Lock and The Dunciad (Oxford World's Classics) i can't make this a hyperlink because of the ' and : symbols, is there somthing i can do to clean the titles before displaying them?

thanks guys

Graham

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: Dealing with ' & : in url
« Reply #1 on: November 22, 2009, 10:22:11 AM »
Check out urlencode().
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline graham23sTopic starter

  • Devotee
  • Posts: 883
    • View Profile
Re: Dealing with ' & : in url
« Reply #2 on: November 22, 2009, 10:29:30 AM »
Hi Cags,

Thnaks mate, i did a test on the title above and the anchor text came out like: The+Major+Works%3A+including+The+Rape+of+the+Lock+and+The+Dunciad+%28Oxford+World%27s+Classics%29-0199537615.html'>The+Major+Works%3A+including+The+Rape+of+the+Lock+and+The+Dunciad+%28Oxford+World%27s+Classics%29

what i am trying to do is make that title a hyperlink to the products page, but because of all the symbols in the title text it cuts me off, simple titles like: The Last Pope work perfect but the majority of amazons titles have symbols in them.

thanks mate

Graham

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: Dealing with ' & : in url
« Reply #3 on: November 22, 2009, 10:38:01 AM »
So whats your point? The text you just posted is the valid url for the text you gave in your previous post. The ampersand and the colon are not 'URL friendly' as they have their own special meaning in an URL. To send those characters via a URL you have to encode them. Which is what urlencode() does.
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline graham23sTopic starter

  • Devotee
  • Posts: 883
    • View Profile
Re: Dealing with ' & : in url
« Reply #4 on: November 22, 2009, 11:08:25 AM »
Hi Mate,

i ended up doing:


function amazon_strip_symbols($stringToClean)
{

  
$new_string preg_replace("/[^a-zA-Z0-9\s]/"" "$stringToClean);

  return 
$new_string;

}


which seems to strip the gunk from the titles pulled back from amazon THEN they are hyperlined to a url for example: www.site.com/title-with-symbol's-book.html <- this symbol (') was crashing the script.

cheers mate

Graham

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: Dealing with ' & : in url
« Reply #5 on: November 22, 2009, 11:14:17 AM »
Well glad you sorted it. I should point out that there's also the urldecode() function which performs the opposite. Amazon themselves are using some form of variant of it, as you'll see if you search for that title you listed, if you look at the keyword attribute in the URL you'll see that the semicolon is replaced by %3A and spaces are replaced by +'s.
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline FaT3oYCG

  • Enthusiast
  • Posts: 280
  • Gender: Male
    • View Profile
    • Modern-Gamer
Re: Dealing with ' & : in url
« Reply #6 on: November 22, 2009, 11:24:13 AM »
amazon are most probably using a rewrite rule, they probably made this custom so people dont scrape their site whihc is what is trying to be done here lol.
Thanks,

Craig.