PHP
regex
img
正则

Get Image Tag From a String

More than 1 year has passed since last update.

The code below can get the img tag from a static string, like this :


/img/portrait.gif?t=1451961935000" align="absmiddle

http://www.example.com/ssss.jpg


function Allimg($str)

{
$pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.bmp|\.jpeg]))[\'|\"].*?[\/]?>/";
preg_match_all($pattern, $str, $match);
$_count = count($match[1]);
$_html = '';
if ($_count>0) {
for ($i = 0; $i < $_count; $i++) {
$_html = $_html . $match[1][$i];
$_html = $_html . '<br>';
}
}
return $_html;
}