织梦dedecms模板代理加盟

2020全新代理计划 赚钱+省钱双管齐下,独立平台,丰厚利润!

您现在的位置: 首页>seo > 站长大学 > 基础技术 > php

php 获取页面中的指定内容类

来源: 发布时间:2020-12-23 11:12:28热度:32℃我要评论(0

免费下载,无需注册无需充值

功能:


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


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


url:<a href="url">xxx</a>


email:admin@admin.com


image:<img data-original="//img.aniys.com/pic.html?zpr=d3c5HsUrFv7sAca6SRL9cAUGkzEJ908G7xadn96Z7f5FXHJhS36M-S-kl1RvyhWnZw10CclEu6ADreJzs-e-" class="lazy" src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=" >




Grep.class.php


<?php

 

class Grep{ // class start

 

    private $_pattern = array(

                            'url' => '/<a.*?href="((http(s)?:\/\/).*?)".*?/si',

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

                            'image' => '/<img.*?data-original="//img.aniys.com/pic.html?zpr=b668naL9KYEPuE-S-6GmQ6kSbYOzW8JtnoSRJWovaC1lWR29jBUCdoGDM3KSsVCXJLebQ5fEmbfNJSunc-e-" class="lazy" src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=" '

                        );

 

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

 

 

   

    public function set($content=''){

        $this->_content = $content;

    }

 

 

   

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

 

        $type = strtolower($type);

 

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

            return array();

        }

 

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

 

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

 

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

 

    }

 

 

   

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

 

        $type = strtolower($type);

 

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

            return $this->_content;

        }

 

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

 

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

 

    }

 

 

   

    private function get_pattern($type){

        return $this->_pattern[$type];

    }

 

 

} // class end

 

?>

Demo

<?php

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

 

require('Grep.class.php');

 

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

 

$obj = new Grep();

$obj->set($content);

 

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

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

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

 

print_r($url);

print_r($email);

print_r($image);

 

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

echo $url_new;

 

function replace_url($matches){

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

}

?> 
责任编辑:迈站网

相关阅读

0大家对 php 获取页面中的指定内容类 的评论