0

I'm using:

$this->tresc[$i][description]=preg_replace("/\<a .*\>.*\<\/a\>/i", "", $this->tresc[$i][description]);

to remove links.

Sometimes the links are having images inside I would like to keep:

<a href="http://www.domain.com/page.php"><img src="http://domain.com/image.jpg" alt="​Image" align="left" /></a>

Is this possible ? Now everything beetween <a> and </a> is removed.

hakre
  • 193,403
  • 52
  • 435
  • 836
Spacedust
  • 931
  • 1
  • 11
  • 22

2 Answers2

7

PHP's strip_tags() function allows you to specify what HTML entities you want to leave untouched.

http://php.net/manual/en/function.strip-tags.php

You can use the optional second parameter to specify tags which should not be stripped.

strip_tags($rssContent, '<img>');

That should remove/sanitize all HTML elements leaving the <img> tags alone.


The comment section of that page in the PHP documentation also contains loads of helpful functions that might be useful to you. I recommend reading through them.
This one specifically looks interesting.

Lix
  • 47,311
  • 12
  • 103
  • 131
2

I did this:

$this->tresc[$i][description]=preg_replace("/<a href=\"(.*)\">/i", "",$this->tresc[$i][description]);
$this->tresc[$i][description]=preg_replace("/<\/a\>/i", "",$this->tresc[$i][description]);

But this is leaving text that's on the link.

Spacedust
  • 931
  • 1
  • 11
  • 22