Jump to content


Photo

remove every image tag in a string? ==SOLVED==


  • Please log in to reply
5 replies to this topic

#1 ale1981

ale1981
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 12 September 2006 - 10:45 AM

This is probably very simple, but I havent got a clue where to start when it comes to preg_replace! What I need is to completely remove any <img> tags from a string and everything in between the tag.

#2 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 September 2006 - 10:49 AM

strip_tags() to remove all html tags.

<?php

$string = preg_replace('/<(\s*)img[^<>]*>/i', '', $string);

?>
to remove just img

#3 ale1981

ale1981
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 12 September 2006 - 11:11 AM

Thanks Jenk will give that a go ;)

#4 ale1981

ale1981
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts

Posted 13 September 2006 - 02:51 PM

Works great ;)

#5 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 13 September 2006 - 02:53 PM

I think I might as well ask this now.  All the str replaces I've seen for filtering out HTML / XML tags have these huge lumps of characters, but I don't know /<(\s*)img[^<>]*>/i  works.  Can you give a slight explanation?

#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 13 September 2006 - 03:07 PM

Jenk: A few nitpicks. Don't use ( ) if you're not going to use what is captured; use (?: ) instead. White space does not need to be searched for before "img", this is invalid HTML.

Can you give a slight explanation?


/
	<img ### Match this literally
	[^>]*  ### Match any character 0 or more times that is not >
	> ### Match this literally
/xi


The /x and /i modifiers are explained here.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users