Hello

I have been all over the web trying to figure out how to do this with no luck. All I want to do is take a string, $story which is comprised of html. What Im looking to do is remove all images from the string and echo the variable, $story again (minus images).

So, if I had:

<img vspace=\"5\" hspace=\"5\" border=\"0\" align=\"left\" src=\"http://www.celebopedia.com/lohan/images/lindsay_lohan.jpg\" />
Lindsay Lohan bugs me

I want it to show:

Lindsay Lohan bugs me

Anyone?>????

    1 - Find the position of "<img" with "strpos()".
    2 - Find the position of ">" using "strpos()" (pass it the above position plus one in the third argument).
    3 - Subtract the first from the second to get the length.
    4 - Use "substr_replace()".

    You could also do it with regular expression functions.

      $story = preg_replace( '#<.*img.*>#msiU', '', $story );
      

        That seems to remove any opening markup:

        $str = '<html><  body><IMG src="xxxx" />text<  img src="yyyy">more text<img src="zzzz" /  ></  BODY></html>';
        echo htmlentities(preg_replace( '#<.*img.*>#msiU', '', $str));
        // Echos "textmore text</  BODY></html>"

        Here's a function that will remove any html element or elements passed to it, along with its closing markup (if any). It will remove inappropiate spaces following opening brackets and closing slashes, and is case insensitive.

        function remove_tag($str, $remove)  // $remove can be scalar or array
        {
            while ((strpos($str, '< ') !== false) || (strpos($str, '/ ') !== false)) {
                $str = str_replace(array('< ', '/ '), array('<', '/'), $str);
            }
            foreach ((array) $remove as $tag) {
                $search_arr = array('<'  . strtolower($tag), '<'  . strtoupper($tag), 
                                    '</' . strtolower($tag), '</' . strtoupper($tag));
                foreach ($search_arr as $search) {
                    $start_pos = 0;
                    while (($start_pos = strpos($str, $search, $start_pos)) !== false) {
                        $end_pos = strpos($str, '>', $start_pos);
                        $len = $end_pos - $start_pos + 1;
                        $str = substr_replace($str, '', $start_pos, $len);
                    }
                }
            }
            return $str;
        }

        Demo:

        $str = '<html><  body><IMG src="xxxx" />text<  img src="yyyy">more text<img src="zzzz" /  ></  BODY></html>';
        
        echo '<pre>' . htmlentities(remove_tag($str, 'img')) . '</pre>';
        // Echos "<html><body>textmore text</BODY></html>"
        
        echo '<pre>' . htmlentities(remove_tag($str, 'body')) . '</pre>';
        // Echos "<html><IMG src="xxxx" />text<img src="yyyy">more text<img src="zzzz" /></html>"
        
        echo '<pre>' . htmlentities(remove_tag($str, array('img', 'body'))) . '</pre>';
        // Echos "<html>textmore text</html>"

          ok, that will help as well

          $story=preg_replace( '#<[^>]*img[^>]*>#msiU', '', $story );
          
            Write a Reply...