Heim >Backend-Entwicklung >PHP-Tutorial >怎么读取<img>里的title
如何读取里的title
在下面的函数中,读取了src,请问如何再读取里的title
function get_pic_list($content='')
{
$pattern="//";
preg_match_all($pattern,$content,$match);
$arr_pic = array();
$i = 0;
foreach($match[1] as $imgsrc)
{
$arr_pic[$i]['src'] = $imgsrc;
$i++;
}
return $arr_pic;
}
------解决思路----------------------
如果不确定是否每张图都有title标记,需要二次正则获取。
修改如下:
<br />function get_pic_list($content='')<br />{<br /> $pattern="/<img .*?src=[\'<br alt="怎么读取<img>里的title" ><font color='#FF8000'>------解决思路----------------------</font><br>\"](.*?(?:[\.gif<br><font color='#FF8000'>------解决思路----------------------</font><br>\.jpg]))[\'<br><font color='#FF8000'>------解决思路----------------------</font><br>\"].*?>/"; <br /> preg_match_all($pattern,$content,$match); <br /><br /> $arr_pic = array();<br /> $i = 0;<br /> for($i=0,$len=count($match[1]); $i<$len; $i++){<br /> $arr_pic[$i]['src'] = $match[1][$i];<br /> preg_match('/title=[\'<br><font color='#FF8000'>------解决思路----------------------</font><br>\"](.*?)[\'<br><font color='#FF8000'>------解决思路----------------------</font><br>\"]/', $match[0][$i], $match1);<br /> $arr_pic[$i]['title'] = isset($match1[1])? $match1[1] : '';<br /> }<br /> return $arr_pic;<br />}<br /><br />$content =<<<TXT<br /><a href="#" class="drop_toggle btn_1 " rel='nofollow'><br /><span><br /> <img src="/assets/ico_set.gif" alt="" title="title1" />管理菜单<img src="/assets/arrow_down.gif" alt="" title="title2" /><br /> <img src="/assets/arrow_down123.gif" alt="" /><br /></span><br /></a><br />TXT;<br /><br />$result = get_pic_list($content);<br /><br />print_r($result);<br />
<br /><br />Array<br />(<br /> [0] => Array<br /> (<br /> [src] => /assets/ico_set.gif<br /> [title] => title1<br /> )<br /><br /> [1] => Array<br /> (<br /> [src] => /assets/arrow_down.gif<br /> [title] => title2<br /> )<br /><br /> [2] => Array<br /> (<br /> [src] => /assets/arrow_down123.gif<br /> [title] => <br /> )<br /><br />)<br />