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 )!