phpregexpreg-match

Get the src value from all img tags


The following regex gets all images src but shows it with src=" at the begining and " at the end of the path.

How can I remove it and show only the path?

preg_match('/src="(.*?)"/i' , $content1 , $matches);

Solution

  • Code:

    $re = '/(?<=src=")(?:.*?)(?=")/ui';
    $txt = '<img src="http://www.gravatar.com/avatar/6f5de9?s=32&amp;d=identicon&amp;r=PG" height="32" width="32" alt="">';
    
    $nMatches = preg_match($re, $txt, $aMatches);
    

    Output:

    Array
    (
        [0] => http://www.gravatar.com/avatar/6f5de9?s=32&d=identicon&r=PG
    )