首页 > 后端开发 > php教程 > php 获取标签之间文本的实现代码

php 获取标签之间文本的实现代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-07-25 08:56:53
原创
1696 人浏览过
本文介绍下,用php实现获取标签之间文本的几个例子,有需要的朋友参考下。

以下例子提供了一种在标签之间检索文本的方法。

注意:不要使用正则表达式解析html。

通过使用正则表达式preg_match()、preg_match_all()函数,这二个函数进行的工作类似于php循环一样,多次遍历得到想要的结果。 另外,使用dom函数可以加快解析速度,得到干净的解析结果。

下面这个例子,使用 preg_match()函数来实现。 代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<?php

 

 /**

 *

 * @get text between tags

 *

 * @param string (The string with tags)

 *

 * @param string $tagname (the name of the tag

 *

 * @return string (Text between tags)

 *

 */

 function getTextBetweenTags($string, $tagname)

 {

    $pattern = "/<$tagname>(.*?)<\/$tagname>/";

    preg_match($pattern, $string, $matches);

    return $matches[1];

 }

?>

登录后复制

注意:以上实现了一个简单的标签获取函数,不过它无法处理嵌套的标签,以及不完整的标签。 通过使用php的dom扩展,可以轻松解决这个问题。

来看下面的例子,函数本身有三个参考: $tag 标签之间的文本 $html 要搜索的HTML或XML $strict 告诉函数加载HTML或XML模式,默认为HTML模式。 第三个参数,允许设置函数来解析XML和一些XHTML文档中发现的自定义标签。

代码:

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

 

/**

 *

 * @get text between tags

 *

 * @param string $tag The tag name

 *

 * @param string $html The XML or XHTML string

 *

 * @param int $strict Whether to use strict mode

 *

 * @return array

 *

 */

function getTextBetweenTags($tag, $html, $strict=0)

{

    /*** a new dom object ***/

    $dom = new domDocument;

 

    /*** load the html into the object ***/

    if($strict==1)

    {

        $dom->loadXML($html);

    }

    else

    {

        $dom->loadHTML($html);

    }

 

    /*** discard white space ***/

    $dom->preserveWhiteSpace = false;

 

    /*** the tag by its tag name ***/

    $content = $dom->getElementsByTagname($tag);

 

    /*** the array to return ***/

    $out = array();

    foreach ($content as $item)

    {

        /*** add node value to the out array ***/

        $out[] = $item->nodeValue;

    }

    /*** return the results ***/

    return $out;

}

?>

登录后复制

在这个例子中,如果使用普通的html,则无需提供第三个参数。 这允许处理无效的、不完整的html标签。缺少关闭的

标签,然而,使用dom和loadHtml可以允许这样的偏差。 这个例子仍然会解析html,并检索一个数组中所有文字之间的所有锚标签。

代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?php

 

$html = '<body>

<h1>Heading</h1>

jbxue.com

<p>paragraph here</p>

<p>Paragraph with a LINK TO jbxue.com</p>

<p>This is a broken paragraph

</body>';

 

$content = getTextBetweenTags('a', $html);

 

foreach( $content as $item )

{

    echo $item.'<br />';

}

?>

登录后复制

在最后的这个例子中,使用两个自定义标签,应用于XML或XHTML文件。 第三个参数被设置为使用XML模式和解析的自定义标签。

代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?php

 

$xhtml = '<html>

<body>

<para>This is a paragraph</para>

<para>This is another paragraph</para>

</body>

</html>';

 

$content2 = getTextBetweenTags('para', $xhtml, 1);

foreach( $content2 as $item )

{

    echo $item.'<br />';

}

?>

登录后复制


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
javascript - input框相关问题
来自于 1970-01-01 08:00:00
0
0
0
angular.js - angular 如何获取标签里的文本?
来自于 1970-01-01 08:00:00
0
0
0
python - Scrapy存在内存泄漏的问题。
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板