tags:

views:

75

answers:

4

Hello,

When I echo out the variable below, spaces are represented as %20.

$row["title"]

So, for instance, if $row["title"] equals "Portugal Crushes North Korea," it echoes out as Portugal%20Crushes%20North%20Korea.

In my source code, how could I replace each %20 with a dash?

Thanks in advance,

John

+1  A: 

Your data is probably url-encoded - use rawurldecode($row['title']) to get it back.

TheDeadMedic
Okay... thanks. What about adding the dashes in place of the %20s ?
John
Actually, it should be rawurldecode. Notice the spaces are not encoded with +.
Artefacto
Nice spot - updated :)
TheDeadMedic
+2  A: 

Both should do

$string = 'Portugal%20Crushes%20North%20Korea';

echo str_replace('%20', '-', $string);
echo str_replace(' ', '-', rawurldecode($string));

Url Decoding the string before replacement isn't strictly necessary though. It just makes sense in case there is additional encoded characters in the string, e.g.:

echo rawurlencode('Portugal Crushes North Korea (7:0)');
// Portugal%20Crushes%20North%20Korea%20%287%3A0%29

With decoding applied, the space to dash replacement would be

Portugal-Crushes-North-Korea-(7:0)

See the PHP Manual

Gordon
Thanks... this looks promising... but it's not working. I'm using this variable as part of a hyperlink. When I hover over the hyperlink, there are spaces where I want the dashes to be. And when I click on the hyperlink, the %20 still appears in the URL where I want the dashes to be.
John
@John `$row['title']` likely does not contain `%20` for spaces in the first place. The browser changes this by itself when you click the link. Try `str_replace(' ', '-', $row['title'])` before creating the link if you want it to have dashes.
Gordon
+1  A: 

how could I replace each %20 with a dash?

str_replace('%20', '-', $row['title']);
Mike B
+2  A: 

Looks like what your doing here is encoding the title before it goes into the database!

i would not do this way, instead do not encode data for DB but encode it as your looping the results to the page, or even better add an extra column called slug.

so you have in title "Some Clean Title Here" and in the slug column you have "some-clean-title-here" so then you use $row['slug']

then in your look use

<a href="site_root.tld/post/<?=$row['slug']?>"><?=$row['title'];?></a>

Always escape your data with mysql_real_escape_string and use a function that dont just urlencode the slug on db entry but also creates a sleek elegant safe formatted string.

the best form for a slig is a-zA-Z0-9 - _ only.

RobertPitt
This seems promising... how do I make the slug variable?
John
For more information about what is slug see this [question](http://stackoverflow.com/questions/427102/in-django-what-is-a-slug), see [this](http://code.google.com/p/php-slugs/) or [this](http://cubiq.org/the-perfect-php-clean-url-generator) for examples in php.
rebus
Thanks RobertPitt... I now am using code that makes a slug and I added a slug column to the database. Now, I'm using the slug as part of the link.
John
Great, by storing the needed versions of the string 1 for user visual's and 1 for url will make your life much easier, always try store the raw data as the user entered it, so you can always do different things to the data, by storing the the changed data you find it harder to do more stuff because you have stored it after you have saved it to DB :)
RobertPitt
also forgot to mention that if your not passing id numbers in your URI Make sure that your slug column is set to UNIQUE so that when you recive the slug from URL you can check it without any dupes.
RobertPitt