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?
Using regular expressions to parse html is usually not recommended.
I think a better approach would be to parse the html server-side, and manipulate it to remove the images or the image src attributes. A library I've had success with is http://simplehtmldom.sourceforge.net/, but I think you can use official PHP DOM extensions.
The removal of background images might be more tricky. You might have to use something like http://www.pelagodesign.com/sidecar/emogrifier/ to apply something like {background: none} to the html elements. However, CSS background images are not supported in the latest versions of Microsoft Outlook, so I would recommend not using them at all from the get-go in order to have the emails to be consistent for most email clients.
You could always do this on the client end as well.
Using this hypothetical code, you should be able to do something like this, pretending that modern browsers all work the same: (or use jQuery or something)
HTMLParser from MDN
I think that the best way to do it and keep the change reversible its using a tag who not process the "src" attribute.
Ex: Change all the "img" with "br"
So print the filtered HTML 1st and reverse it with ajax, search for all the br with a src attribute.
Like tkone mentioned: perhaps JavaScript / jQuery is the answer.
This will look at all images in your preview area and change the source to a placeholder image. The 'placeholder' class sets the background image to the placeholder as well
jQuery
CSS
Not tested, but should work - depending on your setup and needs.
I've asked a similar question (in solution, not actual problem): How to strip specific tags and specific attributes from a string? (Solution)
It's a server side library which cleans (and formats) HTML input according to predefined settings. Have it remove any
src
attributes and allbackground
properties.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:
I've selected the body tags instead of the table, as the
<table>
itself doesn't have abackground
attribute, it only hasbgcolor
. 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 thisUsing all this code togheter, for example if you have a
The PHP will output