3

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

John
  • 4,820
  • 21
  • 62
  • 92

4 Answers4

4

Looks like what you're doing here is encoding the title before it goes into the database!

I would not do it this way. Do not encode data for DB, but encode it as you're looping the results to the page, or even better, add an extra column called slug.

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

Then in your link 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 doesn't just urlencode the slug on db entry, but also creates a sleek, elegant, safely formatted string.

The best form for a slug is a-zA-Z0-9 - _ only.

iff_or
  • 880
  • 1
  • 11
  • 24
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • This seems promising... how do I make the slug variable? – John Jun 25 '10 at 09:35
  • 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. – Davor Lucic Jun 25 '10 at 09:41
  • 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 Jun 25 '10 at 09:55
  • 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 Jun 25 '10 at 09:59
  • 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 Jun 25 '10 at 12:39
3

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
  • 312,688
  • 75
  • 539
  • 559
  • 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 Jun 25 '10 at 09:23
  • @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 Jun 25 '10 at 09:26
2

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

TheDeadMedic
  • 9,948
  • 2
  • 35
  • 50
1

how could I replace each %20 with a dash?

str_replace('%20', '-', $row['title']);
Mike B
  • 31,886
  • 13
  • 87
  • 111