Home > Backend Development > PHP Tutorial > 怎么读取<img>里的title

怎么读取<img>里的title

WBOY
Release: 2016-06-13 12:10:14
Original
826 people have browsed it

如何读取怎么读取<img>里的title里的title
在下面的函数中,读取了src,请问如何再读取怎么读取<img>里的title里的title
function get_pic_list($content='')
{
$pattern="/怎么读取<img>里的title/"; 
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 />
Copy after login


<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 />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template