1

Possible Duplicate:
Grabbing the href attribute of an A element

I have a cms where clients can make their own HTML newsletters. These are send to selected receivers with Swift mailer. Included in these newsletters are images that are linked to online sources. I want to embed these images in the e-mail itself, a feature that Swift mailer supports.

What I like to accomplish is that clients can create their own newsletter including linking to the images. When sending this linked images must be replaced by the code necessary to embed them in the e-mail. Therefor I need to replace the source of the images:

<img src="http://source/to/file.gif" />

to:

<img src="{$message->embed(Swift_Image::fromPath('http://http://source/to/file.gif'))}" />

The HTML content, including the linked images, is stored in a variable $content. I tried to replace the src with:

$content = preg_replace(
    '/<img.*src="(.*?)".*?>/',
    '<img src="' . $message->embed(Swift_Image::fromPath('\1"')) . '" />',
    $content);
Community
  • 1
  • 1
Klaaz
  • 1,590
  • 3
  • 23
  • 46
  • 2
    and correct your regex if you really want to use it. ->use ungreedy (u) marquer to avoid the regex going to the last `'".*?>'` pattern of the document ->use $1 to say "the first pattern captured) ->the string must be : `''` – artragis Mar 16 '12 at 08:10
  • 1
    you are starting the replace string with ' then closing before $message-> with a ", same at the end of string. – kappa Mar 16 '12 at 09:05
  • I added my solution by using a DOM parser. Thanks all for thinking with me. – Klaaz Mar 16 '12 at 09:53
  • @Klaaz the correct way to supplying your own solution is to provide it as answer and not as an edit to your question. Since this is a duplicate anyway you might want to delete your question though because providing your solution as an answer doesnt add any value for future visitors then. – Gordon Mar 16 '12 at 10:00
  • I know, but I have not enough points to add my own answer in 8 hours bacause my status does not allow me to. I will correct this when I am able to provide my own answer (in about 6 hours). – Klaaz Mar 16 '12 at 10:37
  • I wouldn't really count this as a duplicate, because when someone is trying to find a solution for this kind of swiftmailer problem then they most likely wont find the other question! – Gigala Feb 14 '13 at 13:08

1 Answers1

2

I followed the DOM parser suggestion and it works. I am using Simple DOM Parser

To replace all img src's:

require_once("../../simple_html_dom.php");
$content = str_get_html($content);

// Retrieve all img src tags and replace them
foreach($content->find('img') as $e) 
    {
        if($e->src != "") 
            {
                $value = $e->src;
                echo $value;
                $newValue = $message->embed(Swift_Image::fromPath($value)); 
                $e->src = $newValue;
            }
    }

To replace the content of the background tags:

// Retrieve all background tags and replace them
foreach($content->find('td') as $e) 
    {
        if($e->background != "") 
            {
                $value = $e->background;
                $newValue = $message->embed(Swift_Image::fromPath($value)); 
                $e->background = $newValue;
            }
    }
Klaaz
  • 1,590
  • 3
  • 23
  • 46
  • 1
    Note: 'Simple DOM Parser' does not convert back to plain text very well in particular mention to

    tags. I had the issue where it failed to put new lines in, at the moment I am using 'html2text' which does format it to plain text well, available via http://journals.jevon.org/users/jevon-phd/entry/19818

    – toddles_fp Aug 22 '13 at 04:11