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 7781761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:19:51+00:00 2026-06-01T19:19:51+00:00

I am writing a simple HTML email design editor in PHP and also show

  • 0

I am writing a simple HTML email design editor in PHP and also show a demo of how this will look.

I think it would also be very useful to show the user how this will look in an email client such as gmail with images turned off.

What is my best approach for this? Anybody know how this is done in gmail/hotmail etc?

Do I simple remove img -> src and css background: url with a reg expression?

I would like to remove the background parts from:
background="url" used in tables and
background-image:url(url); used inline css

I found this question which has the same kind of idea, although I would like to actually remove the img and backrgound-images from the HTML text.

Or could this code be modified to work with background images also?

  • 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-06-01T19:19:53+00:00Added an answer on June 1, 2026 at 7:19 pm

    I would also suggest using PHP DOM instead of regex, which are often inaccurate. Here is an example code you could use to strip all the img tags and all the background attributes from your string:

    // ...loading the DOM
    $dom = new DOMDocument();
    @$dom->loadHTML($string);  // Using @ to hide any parse warning sometimes resulting from markup errors
    $dom->preserveWhiteSpace = false;
    // Here we strip all the img tags in the document
    $images = $dom->getElementsByTagName('img');
    $imgs = array();
    foreach($images as $img) {
        $imgs[] = $img;
    }
    foreach($imgs as $img) {
        $img->parentNode->removeChild($img);
    }
    // This part strips all 'background' attribute in (all) the body tag(s)
    $bodies = $dom->getElementsByTagName('body');
    $bodybg = array();
    foreach($bodies as $bg) {
        $bodybg[] = $bg;
    }
    foreach($bodybg as $bg) {
        $bg->removeAttribute('background');
    }
    
    $str = $dom->saveHTML();
    

    I’ve selected the body tags instead of the table, as the <table> itself doesn’t have a background attribute, it only has bgcolor.
    To strip the background inline css property, you can use the sabberworm’s PHP CSS Parser
    to parse the CSS retrieved from the DOM: try this

    // Selecting all the elements since each one could have a style attribute
    $alltags = $dom->getElementsByTagName('*');
    $tags = array();
    foreach($alltags as $tag) {
        $tags[] = $tag;
    } $css = array();
    foreach($tags as &$tag) {
        $oParser = new CSSParser("p{".$tag->getAttribute('style')."}");
        $oCss = $oParser->parse();
        foreach($oCss->getAllRuleSets() as $oRuleSet) {
            $oRuleSet->removeRule('background');
            $oRuleSet->removeRule('background-image');
        }
        $css = $oCss->__toString();
        $css = substr_replace($css, '', 0, 3);
        $css = substr_replace($css, '', -2, 2);
        if($css)
            $tag->setAttribute('style', $css);
    }
    

    Using all this code togheter, for example if you have a

    $string = '<!DOCTYPE html>
    <html><body background="http://yo.ur/background/dot/com" etc="an attribute value">
    <img src="http://your.pa/th/to/image"><img src="http://anoth.er/path/to/image">
    <div style="background-image:url(http://inli.ne/css/background);border: 1px solid black">div content...</div>
    <div style="background:url(http://inli.ne/css/background);border: 1px solid black">2nd div content...</div>
    </body></html>';
    

    The PHP will output

    <!DOCTYPE html>
    <html><body etc="an attribute value">
    <div style="border: 1px solid black;">div content...</div>
    <div style="border: 1px solid black;">2nd div content...</div>
    </body></html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing some simple web apps in PHP/HTML/CSS/JS, and am looking for a way
I'm writing a simple html page creator that will generate html code on customized
I'm writing a simple function that split binary/post-data/html from a HTTP response. The HTTP
I am writing a basic http server in C. Handling a simple static .html
I'm writing a simple web app in PHP that needs to have write access
First let me set the situation. I am writing a simple client html page
I'm writing a simple web page using responsive web design, so CSS3 media queries
I'm new to web design, so please forgive if this is super simple- I've
I'm writing a simple tooltip that can hold HTML tags. Please check http://jsfiddle.net/Qkwm8/ for
I am writing a simple drawing app to get an understanding of the HTML

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.