php 擷取頁面中的指定內容類別

來源:互聯網
上載者:User
功能:

1.擷取內容中的url,email,image。

2.替換內容中的url,email,image。

url:<a href="url">xxx</a>email:admin@admin.comimage:<img src="image">



Grep.class.php

<?php/** grep class*   Date:   2013-06-15*   Author: fdipzone*   Ver:    1.0**   Func:**   set:        設定內容*   get:        返回指定的內容*   replace:    返回替換後的內容*   get_pattern 根據type返回pattern*/class Grep{ // class start    private $_pattern = array(                            'url' => '/<a.*?href="((http(s)?:\/\/).*?)".*?/si',                            'email' => '/([\w\-\.]+@[\w\-\.]+(\.\w+))/',                            'image' => '/<img.*?src=\"(http:\/\/.+\.(jpg|jpeg|gif|bmp|png))\">/i'                        );    private $_content = ''; // 源內容    /* 設置搜尋的內容    *  @param String $content    */    public function set($content=''){        $this->_content = $content;    }    /* 擷取指定內容    *  @param String $type    *  @param int    $unique 0:all 1:unique    *  @return Array    */    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();    }    /* 擷取替換後的內容    *  @param String $type    *  @param String $callback    *  @return String    */    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);    }    /* 根據type擷取pattern    *  @param String $type    *  @return String    */    private function get_pattern($type){        return $this->_pattern[$type];    }} // class end?>

Demo



<?phpheader('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]' : '';}?>


以上就是php 擷取頁面中的指定內容類別的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.