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