正则表达提取标签属性值

huangdancs 2012-10-20 03:27:41
如何用正则表达是提title的值“The Amazing Spider-Man (2012)”呢?
我自己是用两次preg_split取的,但感觉很低效,有没有朋友给点思路呢?

<td class="image">
<a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg" height="74" width="54" alt="The Amazing Spider-Man (2012)" title="The Amazing Spider-Man (2012)"></a>
</td>
...全文
258 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄袍披身 2012-10-21
  • 打赏
  • 举报
回复

$html = new simple_html_dom();
$html->load('<td class="image">
<a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg" height="74" width="54" alt="The Amazing Spider-Man (2012)" title="The Amazing Spider-Man (2012)"></a>
</td>');
$title = $html->find('.image a');
print_r($title[0]->attr['title']);


The Amazing Spider-Man (2012)
oAnJing1234 2012-10-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

引用 5 楼 的回复:

引用 4 楼 的回复:

引用 1 楼 的回复:

PHP code

<?php
$str='<td class="image">
<a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="http://ia.media-imdb.com/image……
[/Quote]
preg_match preg_match_all 用法不同造成的
preg_match_all
结果排序为$matches[0]包含第一次匹配得到的所有匹配(包含子组), $matches[1]是包含第二次匹配到的所有匹配(包含子组)的数组, 以此类推.
可以多看看手册 正则方面的 最好有个系统的了解
huangdancs 2012-10-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

引用 4 楼 的回复:

引用 1 楼 的回复:

PHP code

<?php
$str='<td class="image">
<a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4……
[/Quote]

不好意思,可能是我问得不清楚。
我是想知道为什么array[0]的结果是:title="The Amazing Spider-Man (2012)"
而array[1]的结果是:The Amazing Spider-Man (2012)
oAnJing1234 2012-10-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 1 楼 的回复:

PHP code

<?php
$str='<td class="image">
<a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBn……
[/Quote]
你可以把$array1[1]中的数组整合到其他数组里
如$all['title']=$array1[1]
数据取到怎么用就是看需求的了
huangdancs 2012-10-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

PHP code

<?php
$str='<td class="image">
<a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzE……
[/Quote]

谢谢,达到了目的。
但是我不是很清楚是如何往$array和$array1里面存数据的。能指点一下吗?
hmilyhh 2012-10-20
  • 打赏
  • 举报
回复

$string='<td class="image"><a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)1"><img src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg" height="74" width="54" alt="The Amazing Spider-Man (2012)" title="The Amazing Spider-Man (2012)3"></a><a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)2"></td>';
if (preg_match_all ( '/(?<=\s)title="(.*)"/Ui', $string, $arr )) {
var_dump($arr);
}
/*
<br>array(2) {
[0]=>
array(3) {
[0]=>
string(38) "title="The Amazing Spider-Man (2012)1""
[1]=>
string(38) "title="The Amazing Spider-Man (2012)3""
[2]=>
string(38) "title="The Amazing Spider-Man (2012)2""
}
[1]=>
array(3) {
[0]=>
string(30) "The Amazing Spider-Man (2012)1"
[1]=>
string(30) "The Amazing Spider-Man (2012)3"
[2]=>
string(30) "The Amazing Spider-Man (2012)2"
}
}

*/
oAnJing1234 2012-10-20
  • 打赏
  • 举报
回复

<?php
$str='<td class="image">
<a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg" height="74" width="54" alt="The Amazing Spider-Man (2012)" title="The Amazing Spider-Man (2012)"></a>
<a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)">
</td>';
$patten = '/title="(.*)"/Uis';
if (preg_match ( $patten, $str, $array )) {
print_r($array);
}
echo "<br>";
$patten1 = '/<a href=".*" title="(.*)">/Uis';
if (preg_match_all ( $patten1, $str, $array1 )) {
print_r($array1[1]);
}
?>

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧