/    Sign up×
Community /Pin to ProfileBookmark

Replacing spaces (%20) in dynamic urls with hyphen

Hi. Found a few solutions to this but I wasn’t quite sure how to implement it.

The blog I am creating uses php to create the url on a base page as standard which is called blog_single.php. I have used mod_rewrite to change the links to the pages to make them more SEO friendly so now ‘root.com/blog_single.php?id=12&title=article’ becomes ‘root.com/blog/12/article’ using this:

[QUOTE]

RewriteRule ^blog/([^/.]+)/?/([^/.]+)/?$ blog_single.php?id=$1&title=$2 [L]

[/QUOTE]

in .htaccess.

I am then linking to these pages using this code:

[QUOTE]

<?php echo “<a href =”blog/” .$id. “/”.$title. “”>” ?><?php echo $title; ?>

[/QUOTE]

The problem I have with this is that the title displays in the url with spaces ‘root.com/blog/12/article%20title%20here’ where as it was obviously be preferable for me to have hyphens – ‘root.com/blog/12/article-title-here’. I’ve heard of str_replace but i have no idea how to implement this in my existing code.

Would be very grateful if someone could show me how to. Currently titles are entered into the database as they would be read, e.g. ‘Article Title Here’ as a bit of additional information.

(also, if someone would be so kind as to tell me how to remove the article id as from the url, i.e. ‘blog/article-title-here’ instead of ‘blog/12/article-id-here’ that would be fantastic)

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@ZABIJun 15.2011 — <?php echo "<a href ="blog/" .$id. "/".str_replace(' ','-',$title). "">" ?><?php echo $title; ?>
Copy linkTweet thisAlerts:
@dbkJun 15.2011 — Hi

If I understand you correct, you could do like this:

[code=php]
<?php
function replace_whitespace($data)
{
$ready_data = str_replace(" ", "-", $data);
return $ready_data;
}

$new_title = replace_whitespace($title);
?>

<?php echo "<a href ="blog/" .$id. "/".$new_title . "">" ?><?php echo $new_title ; ?>
[/code]
Copy linkTweet thisAlerts:
@dalcorn1authorJun 15.2011 — Thank you both for the replies, ZABIi's method worked. Didn't try yours dbk but I'm sure it was just as good ?. Thank you both again
Copy linkTweet thisAlerts:
@dbkJun 15.2011 — Thank you both for the replies, ZABIi's method worked. Didn't try yours dbk but I'm sure it was just as good ?. Thank you both again[/QUOTE]

Your welcome

Zabis and my methods are identical, except that I made a function that you could use each time needed which will keep your html more simple!
Copy linkTweet thisAlerts:
@NogDogJun 15.2011 — If you're a bit paranoid about other things besides spaces showing up in the URL, you could do something like:
[code=php]
preg_replace('#W+#', '-', $title)
[/code]

[b]W[/b] is any non-word character, e.g not A-Z, a-z, 0-9, or underscore (lower case [b]w[/b] is the inverse, so watch your typing).
Copy linkTweet thisAlerts:
@dalcorn1authorJun 15.2011 — Your welcome

Zabis and my methods are identical, except that I made a function that you could use each time needed which will keep your html more simple![/QUOTE]


ah great thank you
×

Success!

Help @dalcorn1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...