PHP Gets the implementation class for the specified content in the page

Source: Internet
Author: User

  This article is for everyone to use PHP to get the specified content in the page, and to encapsulate the class, you need friends can refer to this article

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:<img src= The "image" >    grep.class.php  code is as follows: <?php /** Grep class  * date:2013-06-15  * Author: fdipzone  * ver:1.0  *  * func:  *  * Set: Set content   * Get: Returns the specified content   * Replace: Returns the replacement content & nbsp * Get_pattern returns pattern  */    class grep{//Class start    Private $_pattern = Array (&nbs) according to type P ' URL ' => '/<a.*?href= ' (http (s)?:/ /).*?)". *?/si ',  ' email ' => '/([w.] +@[w-.] + (. w+))/',  ' image ' => '/<img.*?src= ' (http://.+. ( jpg|jpeg|gif|bmp|png)) ">/i '  );    private $_content = '; Source content      /* Search contents   * @param String $content   */  public Function set ($content = ') {&nbsp ; $this->_content = $content; }     /* Get specified 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); Get pattern    Preg_match_all ($pattern, $this->_content, $matches);    return isset ($matches [1 ])? ($unique ==0? $matches [1]: Array_unique ($matches [1]): Array ();   }     /* get 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 $this->_content; }    $pattern = $this->get_pattern ($type);  & nbsp Return Preg_replace_callback ($pattern, $callback, $this->_content);   }     /* Gets pattern  * @param String $type   * @return string  */  Private Function Get_pattern ($type) {&NB) based on type Sp return $this->_pattern[$type]; } }//class end   ?>    demo    code as follows: < ?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

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.