Question: How to replace %20 characters in URL with a dash (-) when the information is coming from a MySQL db?

Code: href='details.php?listid=$record[listingid]&title=$record[title]

It currently reads:

It should read:

I want to keep the title words for SEO purpose unless you think it is useless??

Thank you - Merci Beaucoup – Muchas Gracias

Natasha ;)

Dani AI

Generated

you do not want to "decode %20 into dashes" in the browser. Instead, generate a dash-separated slug on the server, then place that slug in the URL. Note that urldecode('%20') returns a space, not a dash; browsers also do not rewrite - to %20 because hyphen is an unreserved URI character. Spaces are the ones that must be encoded as %20 (or + in form-style queries). (rfc-editor.org)

A simple, safe PHP pattern is to normalize the title, then build the link. This expands on ’s suggestion with a stricter slug and proper escaping:

$title = $record['title'];                       // e.g. "970 South Bald Head Wynd"
$slug  = preg_replace('/[^A-Za-z0-9]+/', '-', $title);
$slug  = trim($slug, '-');                       // "970-South-Bald-Head-Wynd"

$url = 'details.php?listid='.(int)$record['listingid'].
       '&title='.rawurlencode($slug);            // hyphens stay as-is
echo '<a href="'.htmlspecialchars($url, ENT_QUOTES).'">'.$title.'</a>';

If you want cleaner paths, pair that slug with a rewrite so the URL reads /details/800/970-South-Bald-Head-Wynd while still hitting details.php under the hood:

# .htaccess
RewriteEngine On
RewriteRule ^details/([0-9]+)/([A-Za-z0-9-]+)/?$ details.php?listid=$1&title=$2 [L,QSA]

On the SEO point raised: keeping readable words helps both users and Google. Google explicitly recommends using hyphens to separate words in URLs, as it makes concepts easier to identify; underscores are not recommended. Your example 970-South-Bald-Head-Wynd is exactly the style they show. (developers.google.com)

Practical tips:

  • Keep listid as the source of truth; if the incoming slug does not match your current slug, 301-redirect to the canonical one to avoid duplicates.
  • When outputting links in HTML, always escape & to &amp; (the htmlspecialchars in the snippet handles this). For background on when characters must be percent-encoded in URLs, see MDN’s percent-encoding overview. (developer.mozilla.org)

Recommended Answers

All 7 Replies

Thanks for the link. I have been looking at it and tried different things on my site. It is complicated to say the least.
I was hoping for a simple PHP code for URL decoding (%20 "or space" to a dash).

$str = "%20";
$str2 = urldecode($str);
echo $str2;
-

Note, some browsers may change - to %20 automatically as long as it's in a link, and there's nothing you can do about it, AFAIK.

commented: Best advice - Great code idea - Thanks SCRU +1

You are SOOOOOO right. It is working great in Mozilla but not in IE. THANK YOU so much for your help.
One tiny question do you think exchanging %20 with dashes help with SEO?
Have a fantastic day.

You are SOOOOOO right. It is working great in Mozilla but not in IE. THANK YOU so much for your help.
One tiny question do you think exchanging %20 with dashes help with SEO?
Have a fantastic day.

I don't know that much about SEO.

go for str_replace() or str_ireplace()..it may help u out.

One tiny question do you think exchanging %20 with dashes help with SEO?
Have a fantastic day.

Most of the search engines out there treat dashes as spaces when indexing sites/urls. Hence a search for "order of phoenix" might just show you http://somesite.com/order-of-phoenix but not

That being said, I ain't no expert on SEO, it's just something I heard a while back which I thought I should share. More information here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.