Jump to content

Dealing with ' & : in url


graham23s

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/182502-dealing-with-in-url/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/182502-dealing-with-in-url/#findComment-963237
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182502-dealing-with-in-url/#findComment-963240
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/182502-dealing-with-in-url/#findComment-963252
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182502-dealing-with-in-url/#findComment-963257
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.