Home Backend Development PHP Tutorial 正则表述提取标签属性值

正则表述提取标签属性值

Jun 13, 2016 pm 01:06 PM
quot spider title

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

HTML code

1

2

3

4

5

6

7

8

9

<!--

 

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

 

-->

<td class="image">

    <a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="/static/imghw/default1.png" data-src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg" class="lazy"    style="max-width:90%"  style="max-width:90%" alt="The Amazing Spider-Man (2012)" title="The Amazing Spider-Man (2012)"></a>

  </td>

Copy after login


------解决方案--------------------
PHP code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

<?php $str='<td class="image">

    <a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)"><img src="/static/imghw/default1.png" data-src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg" class="lazy"    style="max-width:90%"  style="max-width:90%" alt="The Amazing Spider-Man (2012)" title="The Amazing Spider-Man (2012)"></a>

    <a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)">

  ';

$patten = '/title="(.*)"/Uis';

if (preg_match ( $patten, $str, $array )) {

print_r($array);

}

echo "<br>";

$patten1 = '/</a><a href=".*" title="(.*)">/Uis';

if (preg_match_all ( $patten1, $str, $array1 )) {

print_r($array1[1]);

}

?&gt;

<br><font color="#e78608">------解决方案--------------------</font><br>

<dl class="code">PHP code<pre class="brush:php;toolbar:false">

 

$string='<td class="image">

<a href="/title/tt0948470/" title="The Amazing Spider-Man (2012)1"><img src="/static/imghw/default1.png" data-src="http://ia.media-imdb.com/images/M/MV5BMjMyOTM4MDMxNV5BMl5BanBnXkFtZTcwNjIyNzExOA@@._V1._SX54_CR0,0,54,74_.jpg" class="lazy"    style="max-width:90%"  style="max-width:90%" 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"></a>

</td>';

    if (preg_match_all ( '/(?array(2) {

  [0]=&gt;

  array(3) {

    [0]=&gt;

    string(38) "title="The Amazing Spider-Man (2012)1""

    [1]=&gt;

    string(38) "title="The Amazing Spider-Man (2012)3""

    [2]=&gt;

    string(38) "title="The Amazing Spider-Man (2012)2""

  }

  [1]=&gt;

  array(3) {

    [0]=&gt;

    string(30) "The Amazing Spider-Man (2012)1"

    [1]=&gt;

    string(30) "The Amazing Spider-Man (2012)3"

    [2]=&gt;

    string(30) "The Amazing Spider-Man (2012)2"

  }

}

 

    */ <div class="clear">

                  

               

               

         

            </div>

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

what does title mean what does title mean Aug 04, 2023 am 11:18 AM

what does title mean

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

What is the meaning of title in HTML What is the meaning of title in HTML Mar 06, 2024 am 09:53 AM

What is the meaning of title in HTML

phpSpider practical tips: How to deal with dynamic loading of web content? phpSpider practical tips: How to deal with dynamic loading of web content? Jul 22, 2023 am 08:33 AM

phpSpider practical tips: How to deal with dynamic loading of web content?

How Scrapy Spider supports multiple web page parsing methods How Scrapy Spider supports multiple web page parsing methods Jun 22, 2023 am 11:17 AM

How Scrapy Spider supports multiple web page parsing methods

phpSpider Advanced Guide: How to implement data crawling that maintains login status? phpSpider Advanced Guide: How to implement data crawling that maintains login status? Jul 21, 2023 pm 04:13 PM

phpSpider Advanced Guide: How to implement data crawling that maintains login status?

Getting started with phpSpider: How to crawl web content easily? Getting started with phpSpider: How to crawl web content easily? Jul 21, 2023 pm 05:46 PM

Getting started with phpSpider: How to crawl web content easily?

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题 Jun 13, 2016 am 10:15 AM

不用数据库来实现用户的简单的下载,代码如下,但是却不能下载,请高手找下原因,文件路劲什么的没有关问题

See all articles