怎么获取php里面的指定内容,PHP语言 获取页面中的指定内容类

本文主要向大家介绍了PHP语言 获取页面中的指定内容类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

功能:

1.获取内容中的url,email,image。

2.替换内容中的url,email,image。

url:xxx

email:admin@admin.com

image:image

Grep.class.php

[php] view plain copy

1. <?php

2. /** grep class

3. *   Date:   2013-06-15

4. *   Author: fdipzone

5. *   Ver:    1.0

6. *

7. *   Func:

8. *

9. *   set:        设置内容

10. *   get:        返回指定的内容

11. *   replace:    返回替换后的内容

12. *   get_pattern 根据type返回pattern

13. */

14.

15. class Grep{ // class start

16.

17.     private $_pattern = array(

18.                             'url' => '/

19.                             'email' => '/([\w\-\.]+@[\w\-\.]+(\.\w+))/',

20.                             'image' => '//i'

21.                         );

22.

23.     private $_content = ''; // 源内容

24.

25.

26.     /* 設置搜尋的內容

27.     *  @param String $content

28.     */

29.     public function set($content=''){

30.         $this->_content = $content;

31.     }

32.

33.

34.     /* 获取指定内容

35.     *  @param String $type

36.     *  @param int    $unique 0:all 1:unique

37.     *  @return Array

38.     */

39.     public function get($type='', $unique=0){

40.

41.         $type = strtolower($type);

42.

43.         if($this->_content=='' || !in_array($type, array_keys($this->_pattern))){

44.             return array();

45.         }

46.

47.         $pattern = $this->get_pattern($type); // 获取pattern

48.

49.         preg_match_all($pattern, $this->_content, $matches);

50.

51.         return isset($matches[1])? ( $unique==0? $matches[1] : array_unique($matches[1]) ) : array();

52.

53.     }

54.

55.

56.     /* 获取替换后的内容

57.     *  @param String $type

58.     *  @param String $callback

59.     *  @return String

60.     */

61.     public function replace($type='', $callback=''){

62.

63.         $type = strtolower($type);

64.

65.         if($this->_content=='' || !in_array($type, array_keys($this->_pattern)) || $callback==''){

66.             return $this->_content;

67.         }

68.

69.         $pattern = $this->get_pattern($type);

70.

71.         return preg_replace_callback($pattern, $callback, $this->_content);

72.

73.     }

74.

75.

76.     /* 根据type获取pattern

77.     *  @param String $type

78.     *  @return String

79.     */

80.     private function get_pattern($type){

81.         return $this->_pattern[$type];

82.     }

83.

84.

85. } // class end

86.

87. ?>

Demo

[php] view plain copy

1. <?php

2. header('content-type:text/htm;charset=utf8');

3.

4. require('Grep.class.php');

5.

6. $content = file_get_contents('http://www.test.com/');

7.

8. $obj = new Grep();

9. $obj->set($content);

10.

11. $url = $obj->get('url', 0);

12. $email = $obj->get('email', 1);

13. $image = $obj->get('image', 1);

14.

15. print_r($url);

16. print_r($email);

17. print_r($image);

18.

19. $url_new = $obj->replace('url', 'replace_url');

20. echo $url_new;

21.

22. function replace_url($matches){

23.     return isset($matches[1])? '[url]'.$matches[1].'[/url]' : '';

24. }

25. ?>

以上就介绍了PHP的相关知识,希望对PHP有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言PHP频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值