Php retrieves the specified content class on the page

Source: Internet
Author: User
1. get the url, email, and image in the content. 2. replace the url, email, and image in the content. Function:

1. get the url, email, and image in the content.

2. replace the url, email, and image in the content.

url:xxxemail:admin@admin.comimage:



Grep. class. php

 '/([\ W \-\.] + @ [\ w \-\.] + (\. \ w +)/', 'image' =>' // I '); private $ _ content = ''; // source content/* set the content of the search criteria * @ param String $ content */public function set ($ content = '') {$ this-> _ content = $ content;}/* get the 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 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 $ this-> _ content ;} $ pattern = $ this-> get_pattern ($ type); return preg_replace_callback ($ pattern, $ callback, $ this-> _ content );} /* obtain pattern * @ param String $ type * @ return String */private function get_pattern ($ type) {return $ this-> _ pattern [$ type];} // class end?>

Demo



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


The above is the content of the specified content class on the php retrieval page. For more information, see PHP Chinese website (www.php1.cn )!

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.