摘要:本文主要向大家介绍了PHP语言 获取页面中的指定内容类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
本文主要向大家介绍了PHP语言 获取页面中的指定内容类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
功能:
1.获取内容中的url,email,image。
2.替换内容中的url,email,image。
url:<a href="url">xxx</a>
email:admin@admin.com
image:<img src="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' => '/<a.*?href="((http(s)?:\/\/).*?)".*?/si',
19. 'email' => '/([\w\-\.]+@[\w\-\.]+(\.\w+))/',
20. 'image' => '/<img.*?src=\"(http:\/\/.+\.(jpg|jpeg|gif|bmp|png))\">/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('//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频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号