ereg and img tag

Posted on 2008-11-13
Last Modified: 2012-05-05
Hi experts

I need to retreive the value of the "src" from the tag "img" into an html string.

for exemple:
html -> this is a test <img src='test.jpg'> test
also html -> this is a test <img src="test.jpg"> test
It should return "test.jpg"
Question by:cabou
    LVL 34

    Expert Comment

    by:Beverley Portlock

    $text = "this is a test <img src='test.jpg'> test";
    $pattern = "(.*)<img src='(.+)'>(.*)";
    $matches = array();

    ereg( $pattern, $text, $matches );

    echo "<br/>";
    echo "The text you what is in the 2nd group and is '". $matches[2] . "'";
    LVL 34

    Expert Comment

    by:Beverley Portlock
    Note: The pattern used above (.*)<img src='(.+)'>(.*) expects <image and src=' to be separated by one space and for thesrc to be delimited by apostrophes rather than double quotes.

    Author Comment

    I need a solution the works with apostrophes and double quotes

    LVL 11

    Accepted Solution

    First, you shouldn't be using the ereg function, it's on its way out. Use PCRE functions instead... preg_match()

    Here's a regular expression that actually works, and actually capture all the images if needed...
    $html = array(
        'blah test <img alt="blue"
    src ="image.jpg" width="12" /> test message',
        '<img src=\'test.jpg\'>',
        '<p>some text<img lowsrc="blackwhite.gif" src="fullcolor.jpg" title="Low/Res"/> <img src="icon.png" height="12"></p>',
    $re = '/<img\s*[^>]*\s+src\s*=\s*([\'"])([^"\']+)\1\s*[^>]*>/im';
    foreach ($html as $h) {
        // Capture all the <img> information
        preg_match_all($re, $h, $matches);
        echo "\r\n".$h.' ===> ';
        echo "\r\n";
        // Captures only the first match
        preg_match($re, $h, $matches);
        echo "\r\n".$h.' ===> '.$matches[2]."\r\n";

    Open in new window


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now