How to make sure your PHP RSS feed reader doesn’t mess up

// March 18th, 2009 // PHP, PHP Tips, Web Development

I was recently working on a custom RSS feed reader where the prior most concern of the client was being able to crop the text with respect to the

  1. Number of words.
  2. Numbers of characters.
  3. Number of sentences.

So a very important measure had to be taken. Since the cropping can end up giving us a broken link if its text  is cropped before the </a> tag, our output can end up as a disaster.

So here is a little script that can help you strip off your html tags in such situations.

<?php

$text = "This is a link to <a href='http://www.fahdmurtaza.com/myblog/'>Fahd Murtaza's </a> site!";
echo strip_tags($text, "");

?>

So strip_tags would end up removing anything from the $text. The second parameter of the strip_tags will simply declare any html tags you want to neglect while stripping the text. Off course you will need to deal with them separately in text cropping situations like I had.

Here is one more example.

<?php
$text
= '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo
strip_tags($text);
echo
"\n";

// Allow <p> and <a>
echo strip_tags($text, '<p><a>');
?>

For more infomation, please check this function’s reference on PHP’s official website.
Also if you are interested in getting the PHP RSS reader script I did, I would encourage you to visit this link  of my next post. Also, if you are intersted in having the script that does the cropping by
  1. Number of words.
  2. Numbers of characters.
  3. Number of sentences.

you can get it here.

Leave a Reply