Tidying Up via PHP

Album Cover: No Line On The Horizon

"I'm running down the road like loose electricity while the band in my head plays a striptease."
U2 / Breathe

Posted on March 25, 2004 6:04 PM in Web Development
Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

After writing about some cool up-and-coming features of PHP 5 involving Tidy yesterday, I got to thinking about the way I typically code my PHP pages. I am guessing I use the same technique as a lot of people, interspersing PHP code in my XHTML, like so:

<p>
 <b>User Comments:</b>
</p>
<p>
 <? print(nl2br($comments)); ?>
</p>

As any experienced PHP programmer knows, as the interspersed PHP code gets more complex (e.g. utilizing while and for loops and dynamic content) it becomes increasingly more difficult to ensure that the XHTML it generates will look pretty (i.e. well structured).

By using the tidy_parse_string() function, you can replace all of your print() calls with string concatenation calls (e.g. $output .= nl2br($comments);) and hold off on outputting any actual XHTML source until you are all done processing via PHP.

When you do this, you save the structuring of your XHTML until the very last step, when your $output variable contains all the data to be output to the browser. Then, when it comes time to print the output, you do something like the following (which is explained a bit on Zend's site):

<?
 $options = array(
  "indent" => true,
  "indent-spaces" => 1,
  "wrap" => 72
 );
 $output = tidy_parse_string($output, $options);
 print($output);
?>

Doing so will, hypothetically speaking, since I haven't had a chance to try this out yet, tidy up all of your XHTML so it looks just as pretty as if you had hand-coded it yourself and didn't require any dynamic processing from PHP.

Pretty nifty, huh?

Comments

blog comments services on September 09, 2016 at 5:43 AM:

I havent any word to value this post.....Really i am inspired from this post....the individual who make this post it was an extraordinary human..thanks for imparted this to us.

Permalink

دكتور اسنان on September 20, 2016 at 10:22 AM:

I definitely really liked every part of it and i also have you saved to fav to look at new information in your site.

Permalink

Goodness, this is truly intriguing perusing. I am happy I discovered this and got the opportunity to peruse it. Incredible employment on this substance. I like it.

Permalink

http://twitter.com/iherb___coupon on September 26, 2016 at 5:33 AM:

I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit.

Permalink

stretch marks on September 26, 2016 at 7:39 AM:

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

Permalink

mobiliario de peluqueria barato on October 05, 2016 at 1:17 AM:

Very good written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing canr wait to read more posts.

Permalink

buy with confidence on October 06, 2016 at 4:40 AM:

Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. There tend to be not many people who can certainly write not so simple posts that artistically. Continue the nice writing

Permalink

hacker team on October 17, 2016 at 1:12 AM:

i am continually searching for some free stuffs over the web. there are likewise a few organizations which gives free specimens. hacker team xbox

Permalink

Cortez on October 22, 2016 at 6:56 PM:

Reading your site gave me a lot of interesting informations

Permalink

Eyelash extension training on October 28, 2016 at 12:30 AM:

Your article perceftly shows what I needed to know, thanks!

Permalink

candid photography sydney on November 02, 2016 at 12:23 AM:

The amateurs must know the important tips to excel in the candid photographer sydney skills.

Permalink

Parallels desktop license key on November 04, 2016 at 5:43 AM:

Excellent blog here! Also your site loads up fast.

Permalink

photography workshops sydney on November 16, 2016 at 12:28 AM:

I would like to thank you for the efforts you have made in writing this article.

Permalink

skip hire brisbane north on November 16, 2016 at 2:07 AM:

I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit.

Permalink

lifestyle blog on December 01, 2016 at 3:02 AM:

Lifestyle blogging will definitely proof fruitful for you. Many bloggers also provides updates against subscription.

Permalink

Halal Restaurant Miami on December 16, 2016 at 1:40 AM:

Very good written article.

Permalink

Urgent Care Near Me on December 16, 2016 at 1:59 AM:

Excellent blog,Thanks for sharing.

Permalink

wholesale iphone charger on December 16, 2016 at 2:55 AM:

Really i am inspired from this post....

Permalink

360 Total Security Keygen on December 30, 2016 at 2:20 AM:

I want you to thank for your time of this wonderful read!!!

Permalink

360 Total Security Keygen on December 30, 2016 at 2:21 AM:

I want you to thank for your time of this wonderful read!!!

Permalink

360 Total Security Keygen on December 30, 2016 at 2:22 AM:

I want you to thank for your time of this wonderful read!!!

Permalink

NewBlueFX Titler Pro on January 05, 2017 at 4:29 AM:

This is a wonderful article, Given so much info in it

Permalink

futon bed on January 12, 2017 at 5:12 AM:

Here are futon bed to bring the best quality mattress for a peaceful sleep at day's end.

Permalink

Tweakbit fixmypc on February 09, 2017 at 2:58 AM:

I appreciate all the work you put into this site, helping out others with your fun and creative works.

Permalink

MB2-714 braindumps on March 13, 2017 at 2:57 AM:

Hey admin your sharing way is unique. This is fantastic site.I enjoyed reading your articles. This is truly a great read for me.

Permalink

Iobit uninstaller on March 26, 2017 at 4:28 AM:

I was interested in this topic. Thank you for sharing the link here

Permalink

Connectify hotspot pro on March 29, 2017 at 3:54 AM:

Great post,Thanks a lot for the kind of perfect topic I have not a lot of information about it but I have got an extra unique info in your unique post.

Permalink

Toyota Supra 2018 on April 04, 2017 at 12:33 AM:

Read the latest Toyota Supra sports car news, photos and Release Date.

Permalink

Proshow producer on April 09, 2017 at 6:17 AM:

Nice Web site, Continue the beneficial work. With thanks!. Super inarfmotive writing; keep it up.

Permalink

swissarmyknife on April 10, 2017 at 4:13 AM:

THE POST IS TRANSCENDENT swiss army knives

Permalink

spyhunter 4 key on April 11, 2017 at 11:15 PM:

innovative post with great information, i like it very much

Permalink

Fileviewpro on April 12, 2017 at 6:16 AM:

Very nice post.thanks for sharing such an effective post.

Permalink

Urgent Printing London on April 21, 2017 at 3:51 AM:

Very interesting blog. Alot of blogs I see these days dont really provide anything that Im interested in, but Im most definately interested in this one.

Permalink

Beautiful engagement rings on April 23, 2017 at 4:17 AM:

I am trusting the same best exertion from you later on too. Actually your exploratory writing abilities has roused me.

Permalink

Internet Download Manager Crack on April 26, 2017 at 5:02 AM:

Really amazing, Your articles were very nice and full of information. Good Job And continue to make useful articles

Permalink

Buy barcodes on April 27, 2017 at 4:16 AM:

Great Job. The article is very interesting to read. I enjoyed it to the last word. payung wagomu

Permalink

crack81 on May 06, 2017 at 6:00 AM:

Your blog is very nice thanks for sharing Then justVery nice, thanks for sharing to us Enjoyed every bit of your blog.Really looking forward to read more.

Permalink

crack82 on May 06, 2017 at 6:07 AM:

Nice post! Thanks a lot for the kind of perfect topic.I like your post. Thanks for sharing.

Permalink

crack4activator on May 06, 2017 at 6:09 AM:

I was reading the provided article with a great interest! This post is useful and informative.

Permalink

crack77 on May 06, 2017 at 6:11 AM:

I'm always happy to follow this blog post. cool various posts here on friends in social media, various comments always fulfill my social media posts. many benefits, thanks writer, keeps the spirit.

Permalink

crack44 on May 06, 2017 at 6:12 AM:

I'm always happy to follow this blog post. cool various posts here on friends in social media, various comments always fulfill my social media posts. many benefits, thanks writer, keeps the spirit.

Permalink

crack88 on May 06, 2017 at 6:13 AM:

Nice post! Thanks a lot for the kind of perfect topic.I like your post. Thanks for sharing.

Permalink

img src on May 10, 2017 at 5:12 AM:

thanks for the info and also try this free image hosting site

Permalink

geometric terrarium jar on May 18, 2017 at 12:10 AM:

Really i appreciate the effort you made to share the knowledge. This is really a great stuff for sharing. Keep it up . T
hanks for sharing.

Permalink

2017 hummer h3 on May 18, 2017 at 10:34 PM:

Nice information, very interesting. I'm very glad to find this site.

Permalink

unblock youtube on May 27, 2017 at 3:53 AM:

finally found great post here.I will get back here. I just added your blog to my bookmark sites. thanks.Quality posts is the crucial to invite the visitors to visit the web page, that’s what this web page is providing. unblock youtube

Permalink

Post Comments

If you feel like commenting on the above item, use the form below. Your email address will be used for personal contact reasons only, and will not be shown on this website.

Name:

Email Address:

Website:

Comments:

Check this box if you hate spam.