www.webdeveloper.com
Results 1 to 3 of 3

Thread: Help with pattern (preg_match_all)

Hybrid View

  1. #1
    Join Date
    Jul 2010
    Posts
    71

    Help with pattern (preg_match_all)

    Hi

    I'm working on a class to get mails and saves it i an archive.
    To grab the inlined images of the message i use 'preg_match_all' to grab the path to the images, which will be changed later.
    It works perfect in this example:
    PHP Code:
    $message '<img width="220" height="43" id="_x0000_i1028" src="cid:image001.jpg@01CDAC83.10DC03C0" alt="Data Gården logo">';

    $SearchImageName 'image001.jpg';

    preg_match_all('/src="cid:('.$SearchImageName.')(.*)"/U'$message$inline_match_array[]); 
    In this example the above search pattern does not work because it only has a wildcard behind the $SearchImageName. But I can't make it work with a wildcard in front of the $SearchImageName.
    I also have a problem with the file extension has been changed to .ashx instead of .jpg :

    PHP Code:
    $message '<img id="content_0_img" src="http://danskeark.dk/~/media/Dark/Nyheder/Fokus-fotos/2012_10_17_fokus%20kopi.ashx?mw=470&amp;as=1">'

    $SearchImageName '2012_10_17_fokus kopi.jpg'
    Any ideas for a search pattern?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,227
    It might be "nicer" to find a way to do this with the DOM extension, but this ugly bunch of code seems to work and cover most possibilities:
    PHP Code:
    <?php

    $message 
    '<img id="content_0_img" src="http://danskeark.dk/~/media/Dark/Nyheder/Fokus-fotos/2012_10_17_fokus%20kopi.ashx?mw=470&amp;as=1">';

    $SearchImageName '2012_10_17_fokus kopi.';

    $findMe '('.preg_quote($SearchImageName).'|'.
            
    preg_quote(urlencode($SearchImageName)).'|'.
            
    preg_quote(rawurlencode($SearchImageName)).')';
    $regexp '/src=["\']([^"\']*'.$findMe.')([^"\']*)["\']/i';
    //echo "<pre>".htmlspecialchars($regexp)."</pre>\n";

    preg_match_all($regexp$message$inline_match_array);

    echo 
    "<pre>".print_r($inline_match_array1)."</pre>";
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jul 2010
    Posts
    71
    Thanks NogDog

    Looking forward to test the code!

    I never really looked into the DOM thing you mention, but maybe it's about time.

    Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles