mahonglin123456 发表于 2018-12-11 14:26:42

php正则匹配类


[*]代码:
/**
* @desc:正则匹配类
* @author
* @method
* 1、geturl         获取所有超链接
* 2、getimg         获取所有图片
* 3、getaudio         获取所有音频文件
* 4、getvideo         获取所有视频文件
* 5、getparagraph       获取所有段落
* 6、getuser            获取自定义内容         preg 自定义正则,如:/(.*)/Ui
*/
class match{
private $content = '';
/*
@desc:构造方法,初始化待匹配文本
*/
public function __construct($content){
$this->content = $content;
}
/*
@desc:获取所有超链接
@return:所有匹配的超链接
*/
public function geturl(){
$content = $this->content;
$preg = '/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;
}
return array_unique($videos);
}
/*
@desc:获取所有段落文本
@return:所有匹配的段落文本
*/
public function getparagraph(){
$content = $this->content;
$preg="/(.*)/Ui";
$bool = preg_match_all($preg,$content,$res);
$paragraphs = array();
if($bool){
$paragraphs = $res;
}
return array_unique($paragraphs);
}
/*
@desc:获取所有自定义内容
@return:所有匹配的自定义内容
*/
public function getuser($preg){
$content = $this->content;
$bool = preg_match_all($preg,$content,$res);
$users = array();
if($bool){
$users = $res;
}
return array_unique($users);
}
}
[*]测试:
$match = new match($str);
$ret = $match->getimg();
var_dump($ret);
[*]输出:
array(7) {
=>
string(61) "https://assets-cdn.github.com/images/search-shortcut-hint.svg"
=>
string(69) "https://assets-cdn.github.com/images/spinners/octocat-spinner-128.gif"
=>
string(75) "https://assets-cdn.github.com/images/spinners/octocat-spinner-32-EAF2F5.gif"
=>
string(68) "https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif"
=>
string(45) "https://badges.gitter.im/walkor/Workerman.svg"
=>
string(48) "http://www.workerman.net/img/workerman-start.png"
=>
string(42) "http://donate.workerman.net/img/donate.png"
}



页: [1]
查看完整版本: php正则匹配类