功能:
1.获取内容中的url,email,image。
2.替换内容中的url,email,image。
url:<a href="url">xxx</a>
email:admin@admin.com
image:<img data-original="//img.aniys.com/pic.html?zpr=d3c5HsUrFv7sAca6SRL9cAUGkzEJ908G7xadn96Z7f5FXHJhS36M-S-kl1RvyhWnZw10CclEu6ADreJzs-e-" class="lazy" src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=" >
Grep.class.php
<?php
class Grep{ // class start
private $_pattern = array(
'url' => '/<a.*?href="((http(s)?:\/\/).*?)".*?/si',
'email' => '/([\w\-\.]+@[\w\-\.]+(\.\w+))/',
'image' => '/<img.*?data-original="//img.aniys.com/pic.html?zpr=b668naL9KYEPuE-S-6GmQ6kSbYOzW8JtnoSRJWovaC1lWR29jBUCdoGDM3KSsVCXJLebQ5fEmbfNJSunc-e-" class="lazy" src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs=" '
);
private $_content = ''; // 源内容
public function set($content=''){
$this->_content = $content;
}
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); // 获取pattern
preg_match_all($pattern, $this->_content, $matches);
return isset($matches[1])? ( $unique==0? $matches[1] : array_unique($matches[1]) ) : array();
}
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);
}
private function get_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]' : '';
}
?>