Removing user inserted image tags with PHP
<p>I am trying to remove any user inserted image tags while allowing my own image icon. I have this</p>

<pre><code>$post = 'Here is an image &lt;img src="imgage.jpg" /&gt; to check
         and my icon &lt;img src="/images/ImageLink.jpg" /&gt;';

$imgcheck = true;
$stringstart = 0;

while($imgcheck == 'true'){
    if($stringstart = strpos($post,'&lt;img',$stringstart)){
        if ($stringend = strpos($post,'&gt;',$stringstart)){
            $strlength = $stringend - $stringstart;
            $substring = substr($post,$stringstart,$strlength);
            if (!preg_match('/src="\/images\/ImageLink.jpg"/',$substring)){
                $post = str_replace($substring, "", $post);
            }
            else{
                $stringstart = $stringend;
            }

        }
        else{
            $imgcheck = 'false';
        }
    }
    else{
            $imgcheck = 'false';
    }

}
</code></pre>

<p>I would like this to return</p>

<pre><code>Here is an image to check and my icon &lt;img src="/images/ImageLink.jpg" /&gt;
</code></pre>

<p>I seem to be getting an error on one of the strpos functions but cannot figure out why. </p>

<p>Update:Thank you hakre. That answer is much more elegant. I did finally make the code work with </p>

<pre><code>$imgcheck = true;
$stringstart = 0;

while($imgcheck == 'true'){
    if($stringstart = strpos($post,'&amp;lt;img',$stringstart)){
        if ($stringend = strpos($post,'&amp;gt;',$stringstart)){
            $strlength = $stringend - $stringstart +4;
            $substring = substr($post,$stringstart,$strlength);
            if (!preg_match('~src="\/images\/ImageLink.jpg"~',$substring)){
                $post = str_replace($substring, "", $post);
            }
            else{
                $stringstart = $stringend;
            }

        }
        else{
            $imgcheck = 'false';
        }
    }
    else{
            $imgcheck = 'false';
    }

}
</code></pre>

<p>with <code>&amp;lt;</code> and <code>&amp;gt;</code> used because I am using a div with contenteditable=true for user input. This could also be used for a text area by simply replacing <code>&amp;lt;</code> and <code>&amp;gt;</code> with <code>&lt;img</code> and <code>&gt;</code> respectively as well as changing </p>

<pre><code>$strlength = $stringend - $stringstart +4;
</code></pre>

<p>to </p>

<pre><code>$strlength = $stringend - $stringstart +1;
</code></pre>

<p>Thank you for your time.</p>
