Home  >  Article  >  php教程  >  php中正则获取url函数preg_match

php中正则获取url函数preg_match

WBOY
WBOYOriginal
2016-06-13 10:03:25898browse

下面利用preg_match举了两个实例一个是获取指定区域内容,一个是获取内容中的url地址。

下面利用preg_match举了两个实例一个是获取指定区域内容,一个是获取内容中的url地址。
*/
$string="example:

this is a test
";   //定义字符串
$pattern="|]+>(.*)[^>]+>|u";        //定义正则表达式模式
/*该匹配模式的意义是:以""的字符加上结尾内容;子模式中的".*"表示0到多个任意字符,再加上以""的字符加上">"符号。*/
preg_match_all($pattern,$string,$out,preg_pattern_order);  //进行preg_mathc_all处理
echo $out[0][0]; 
echo ",";
echo $out[0][1];
echo "

";
echo $out[1][0];
echo ",";
echo $out[1][1];

//实例二

//以下代码用于提取域名
$http="http://www.bKjia.c0m";       //定义网址
preg_match("/^(http://)?([^/]+)/i",$http,$matches);     //进行正则表达式匹配
$host=$matches[2];           //把结果数组元素赋值到变量
preg_match("/[^./]+.[^./]+$/",$host,$matches);     //进行正则表达式匹配
echo "domain name is:{$matches[0]}n";       //输出结果域名

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn