Gets the PHP class for the content specified in the page

Source: Internet
Author: User
Tags php class

Function:

1. Get the url,email,image in the content.

2. Replace the url,email,image in the content.

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

Email:admin@admin.com

Image:

Grep.class.php

<?php/** grep class * date:2013-06-15 * author:fdipzone * ver:1.0 * * Func: * * Set:  Set content * Get: Returns the specified content * Replace: Returns the replaced content * Get_pattern returns pattern/class grep{/class based on type Start private $_pattern = array (' URL ' => '/<a.*?href= ' (http (s)?: \ /\/).*?)". *?/si ', ' email ' => '/([\w\-\.] +@[\w\-\.] + (\.\w+))/', ' image ' => '//i"); Private $_content = ';  
        SOURCE content * @param String $content/Public Function set ($content = ') {  
    $this->_content = $content; /* Get the specified content * @param String $type * See more highlights in this column: HTTP://WWW.BIANCENG.CNHTTP://WWW.BIANCENG.C
   n/webkf/php/* @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);  
      
        Get pattern Preg_match_all ($pattern, $this->_content, $matches); return Isset ($matches [1])?  
      
    ($unique ==0 $matches [1]: Array_unique ($matches [1])): Array (); 
    /* Get the Replaced content * @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 $thi  
        s->_content;  
      
        $pattern = $this->get_pattern ($type); Return Preg_replace_callback ($pattern, $caLlback, $this->_content); /* Get pattern * @param string $type * @return string/Private function g based on type  
    Et_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] ': ';  
}  
? >
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.