Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7028241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:21:40+00:00 2026-05-28T00:21:40+00:00

I am trying to remove any user inserted image tags while allowing my own

  • 0

I am trying to remove any user inserted image tags while allowing my own image icon. I have this

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

$imgcheck = true;
$stringstart = 0;

while($imgcheck == 'true'){
    if($stringstart = strpos($post,'<img',$stringstart)){
        if ($stringend = strpos($post,'>',$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';
    }

}

I would like this to return

Here is an image to check and my icon <img src="/images/ImageLink.jpg" />

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

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

$imgcheck = true;
$stringstart = 0;

while($imgcheck == 'true'){
    if($stringstart = strpos($post,'&lt;img',$stringstart)){
        if ($stringend = strpos($post,'&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';
    }

}

with &lt; and &gt; 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 &lt; and &gt; with <img and > respectively as well as changing

$strlength = $stringend - $stringstart +4;

to

$strlength = $stringend - $stringstart +1;

Thank you for your time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:21:41+00:00Added an answer on May 28, 2026 at 12:21 am

    You should use a HTML parser for the job, for example DOMDocument.

    The following example code will put your html fragment into a div with the id post so it can be identified:

    $post = 'Here is an image <img src="imgage.jpg" /> to check
             and my icon <img src="/images/ImageLink.jpg" />';
    
    $post = '<div id="post">'.$post.'</div>';
    

    Then the DOMDocument is created and the html loaded into it. Additionally the container div is made available as a variable by it’s id:

    $doc = new DOMDocument();
    $doc->loadHTML($post);
    $container = $doc->getElementById('post');
    

    The next step is to select all images you want to remove. This is done with xpath. The xpatch expression will then be queried with the $container as reference:

    $xpath = new DOMXPath($doc);    
    $expression = './/img[@src != "/images/ImageLink.jpg"]';
    $nodes = $xpath->query($expression, $container);
    

    The next step is to iterate over all found elements and to remove them:

    foreach($nodes as $node)
    {
        $parent = $node->parentNode;
        $parent->removeChild($node);
    }
    

    Finally the changed HTML needs to be aquired by taking everything out of the $container:

    $innerHTML = '';
    foreach($container->childNodes as $child) {
        $innerHTML .= $node->ownerDocument->saveXML($child);
    }
    

    Now everything is in

    echo $innerHTML;
    

    which will give you:

    Here is an image  to check
             and my icon <img src="/images/ImageLink.jpg"/>
    

    Hope this is useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to remove a class once the user hovers over a link. Here
The following code is trying to remove any duplicate characters in a string. I'm
I'm trying to remove some items of a dict based on their key, here
I am trying to customize this script so that instead of a user's recent
i am trying to change image in the div if the user click on
I'm pulling my hair out trying to resolve this so any tips and help
In my application I have robots that the user can give any number of
I have been trying to develop some user controls, one being a text edit
I am trying to make a program about recipes. The user would have to
I'm trying to implement key-press functionality which will remove a div when the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.