Categories: Tips & Tricks

Remove spaces when echoing the_title

This little trick can be useful for calling custom functions and printing the title with out spaces for W3C compatibility. I used this trick in a new theme called Galleria, which will be out for public download in the coming days.

Using this comes in handy for a delicious text link:

<?php $title = get_the_title(); ?>
<a href="http://del.icio.us/post?url=<?php echo $title; ?>&amp;<?php echo str_replace(" ", "%20", $title); ?>">
Bookmark This (<?php echo $title; ?>)</a>

What I am doing is calling $title = get_the_title(); and using str_replace(" ", "%20", $title); to replace empty spaces with a %20, which is used in URL encoding empty spaces.

Alternatively you can use a dash or underscore.

Thanks to Jason Boyle for his adaption.

Web Master

Hi, I am Miguel, I bought this site in 2009. So I now run or manage the site. Please visit my new website or follow me on twitter @W3i.

View Comments

Recent Posts

Beat Recession Blues with These PHP Web Development Best Practices

With the recession taking over the majority of economies across the globe, securing your job…

5 hours ago

The Best Christmas Apps for iPhone 5

The Christmas spirit is often one that takes a little while to get into, however,…

7 hours ago

5 Unique Business Card Designs Your Clients Will Love

Business cards are an excellent way to share your information, especially if you're self-employed. The…

19 hours ago

8 Content Ideas To Boost Your Real Estate Blog

When you have a real estate blog, sometimes it can be difficult to come up…

1 day ago

A Tweeter Guide to Branding Marketing Stars-Luminaries You Should Follow

When it comes to your business, marketing is extremely important. With the popularity of social…

2 days ago

Lost? Since You Can’t Trust Apple Maps, Check Out These 3 Apps Instead

Maybe you have the new software on your Apple device that has made headlines as…

2 days ago