MariaL
MariaL

Reputation: 1242

Preg_replace to replace html tags apart from img tags

I've got a function to limit the amount of words that are pulled from the rss feed description of a blog post. It also ignores any html tags. This is the code

<?php
function limitWords($s) {
return preg_replace('/((\w+\W*){79}(\w+))(.*)/', '${1}', $s);    
}
 ?>

<a title="<?php echo $item->title;?>" href="<?php echo $item->link; ?>"><?php echo limitWords($item->description()); ?> <?php echo '....' ?></a>

Now in some of the descriptions there are images though and I need to display those. Is it possible to not replace img tags in my above function? so to replace all html tags APART from <img> Any ideas?

Upvotes: 0

Views: 46

Answers (1)

Marcel
Marcel

Reputation: 5119

The native php strip_tags function can do that for you. There 's no need to parse a string with regular expressions. Have a look at the following example:

$sYourString = '<a href="" title=""><img src="" alt=""><span>text</span></a>';
$sStrippedString = strip_tags($sYourString, '<img>');

That piece of code is untestet. But it should work for you.

Upvotes: 1

Related Questions