/**
* @desc:正則匹配類
* @author [Lee] <[]>
* @method
* 1、geturl 獲取所有超鏈接
* 2、getimg 獲取所有圖片
* 3、getaudio 獲取所有音頻文件
* 4、getvideo 獲取所有視頻文件
* 5、getparagraph 獲取所有段落
* 6、getuser 獲取自定義內(nèi)容 preg 自定義正則,如:/(.*)/Ui
*/
class match{
private $content = '';
/*
@desc:構(gòu)造方法,初始化待匹配文本
*/
public function __construct($content){
$this->content = $content;
}
/*
@desc:獲取所有超鏈接
@return:所有匹配的超鏈接
*/
public function geturl(){
$content = $this->content;
$preg = '/<[a|A].*?href=[\'\"]{0,1}([^>\'\"\ ]*).*?>/i';
$bool = preg_match_all($preg,$content,$res);
$urls = array();
if($bool){
$urls = $res[1];
}
return array_unique($urls);
}
/*
@desc:獲取所有圖片
@return:所有匹配的圖片
*/
public function getimg(){
$content = $this->content;
$preg="/(src)=(\\\?)([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png|svg))\\2\\3/i";
$bool = preg_match_all($preg,$content,$res);
$imgs = array();
if($bool){
$imgs = $res[4];
}
return array_unique($imgs);
}
/*
@desc:獲取所有音頻文件
@return:所有匹配的音頻文件
*/
public function getaudio(){
$content = $this->content;
$preg="/(src)=(\\\?)([\"|']?)([^ \"'>]+\.(mp3|wav|wma|ogg|ape|acc))\\2\\3/i";
$bool = preg_match_all($preg,$content,$res);
$audios = array();
if($bool){
$audios = $res[4];
}
return array_unique($audios);
}
/*
@desc:獲取所有視頻文件
@return:所有匹配的視頻文件
*/
public function getvideo(){
$content = $this->content;
$preg="/(src)=(\\\?)([\"|']?)([^ \"'>]+\.(swf|flv|mp4|rmvb|avi|mpeg|ra|ram|mov|wmv)((\?[^ \"'>]+)?))\\2\\3/i";
$bool = preg_match_all($preg,$content,$res);
$videos = array();
if($bool){
$videos = $res[4];
}
return array_unique($videos);
}
/*
@desc:獲取所有段落文本
@return:所有匹配的段落文本
*/
public function getparagraph(){
$content = $this->content;
$preg="/
(.*)<\/p>/Ui";
$bool = preg_match_all($preg,$content,$res);
$paragraphs = array();
if($bool){
$paragraphs = $res[1];
}
return array_unique($paragraphs);
}
/*
@desc:獲取所有自定義內(nèi)容
@return:所有匹配的自定義內(nèi)容
*/
public function getuser($preg){
$content = $this->content;
$bool = preg_match_all($preg,$content,$res);
$users = array();
if($bool){
$users = $res[1];
}
return array_unique($users);
}
}
$match = new match($str);
$ret = $match->getimg();
var_dump($ret);
array(7) {
[0]=>
string(61) "https://assets-cdn.github.com/images/search-shortcut-hint.svg"
[1]=>
string(69) "https://assets-cdn.github.com/images/spinners/octocat-spinner-128.gif"
[2]=>
string(75) "https://assets-cdn.github.com/images/spinners/octocat-spinner-32-EAF2F5.gif"
[3]=>
string(68) "https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif"
[14]=>
string(45) "https://badges.gitter.im/walkor/Workerman.svg"
[15]=>
string(48) "/upload/otherpic47/106945.jpg"
[16]=>
string(42) "http://donate.workerman.net/img/donate.png"
}