We help IT Professionals succeed at work.

filter text string to remove <img> tags

How does one filter a text string to determine whether the text string contains <img> tags? I want to remove image tags from the following text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. <img src="myImage.jpg" />  Donec ac lacus nunc. Donec at sagittis massa.
Comment
Watch Question

Web Expert
Commented:
use stip_tags function of php.

It will remove html tags from the provided string.

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

Hope this helps
Addy
Commented:
Hi just have a look at the following code

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

?>
output:
Test paragraph. Other text

Author

Commented:
yeah, this works perfectly. Thanks.

Do you know how to split the string and start with the second phrase after the <img> tag. For example, if I wanted to start at the first <p> tag or "Donec ac lacus nunc."... like this:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. <img src="myImage.jpg" />  <p>Donec ac lacus nunc. Donec at sagittis massa.</p>

Thanks again!

Author

Commented:
thanks

Commented:
Other than using strip_tags , you can use string explode, implode to write custom function for it..

Let
$img_string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. <img src="myImage.jpg" />  Donec ac lacus nunc. Donec at sagittis massa."

if you want to remove <img> tag completely from a string of given example :
 I assumed only img tags will be there in string other than normal text.

if so,
$img_str_array = array();
$img_str_array = explode("<img ", $img_string);
$new_img_string = $img_str_array[0];

for($i=1;$i<count($img_str_array);$i++)
{
   $img_arr1 = array();  
   $img_arr1 = explode("/>", $img_str_array[$i]);
         
   $new_img_string .= $img_arr1[1];
}
   
echo $new_img_string;

 -- Now you may customize this to work with your specific requirement...

Hope this helps..

--
YRuZ!



}