正则表达式和字符串处理

本文详细解析了如何使用PHP正则表达式匹配并获取网址中的协议、主机、域名、顶级域名、文件路径等关键信息。通过实例演示了正则表达式的应用,并展示了如何批量处理多个网址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php 
   $pattern='/(http?|ftp?):\/\/(www)\.([^\.\/]+)\.(com|net|org)(\/[\w-\.\/\?\%\&\=]*)?/i';
   $subject="网址为http://www.lampbrother.net/index.php的位置是LAMP兄弟连";
   if(preg_match($pattern,$subject,$matches))
   {
   	echo"搜索的URL为:".$matches[0]."<br>";
   	echo"URL中的协议为:".$matches[1]."<br>";
   	echo"URL中的主机为:".$matches[2]."<br>";
   	echo"URL中的域名为:".$matches[3]."<br>";
   	echo"URL中的顶域为:".$matches[4]."<br>";
   	echo"URL中的文件为:".$matches[5]."<br>";
   }else {
   	echo "搜索失败!";
   }
   
   $str="网址为http://www.lampbrother.net/index.php的位置是LAMP兄弟连,
                       网址为http://www.baidu.com/index.php的位置是百度,
                       网址为http://www.google.com/index.php";
   $i=1;
   if(preg_match_all($pattern,$str,$matches,PREG_SET_ORDER))
   {
   	foreach ($matches as $sur)
   	{
   	echo"搜索到第".$i."个URL为:".$sur[0]."<br>";
   	echo"第".$i."个URL中的协议为:".$sur[1]."<br>";
   	echo"第".$i."个URL中的主机为:".$sur[2]."<br>";
   	echo"第".$i."个URL中的域名为:".$sur[3]."<br>";
   	echo"第".$i."个URL中的顶域为:".$sur[4]."<br>";
   	echo"第".$i."个URL中的文件为:".$sur[5]."<br>";
   	$i++;
   	}
   }else {
   	echo "搜索失败!";
   }
   
   $ar=array("linux redhat9.0","apache2.2.9","mysql5.0.51","php5.2.6","lamp","100");
   $version=preg_grep("/^[a-zA-Z]+(\d|\.)+$/",$ar);
   print_r($version);
   
   echo"<br>";
   echo strstr("this is a test","test");
   echo "<br>";
   echo strstr("this is a test",115);
   echo"<br>";
   
   function getfile($url)
   {
   	$local=strrpos($url,"/")+1;
   	$file=substr($url,$local);
   	return $file;
   }
   echo getfile("http://bbs.lampbrother.net/index.php");
   echo"<br>";
   echo getfile("file:///C:/WINDOWS/php.ini");
   echo"<br>";
   echo getfile("http://bbs.lampbrother.com/image/sharp/logo.gif");
   echo"<br>";
   
   $pa="/<[\/\!]*?[^<>]*?>/is";
   $test="这个文本有<b>粗体</b>和<u>带有下划线</u>以及<i>斜体</i>还有<font color='red' size='7'>带有颜色和字体大小</font>的标记";
   echo preg_replace($pa,"",$test);
   echo "<br>";
   echo preg_replace($pa,"",$test,2);
   
   $pat="/(\d{2})\/(\d{2})\/(\d{4})/";
   $text="今年春节假期为01/25/2009到02/02/2009共七天";
   echo "<br>";
   echo preg_replace($pat,"\\3-\\1-\\2",$text);
   echo "<br>";
   echo preg_replace($pat,"\${3}-\${1}-\${2}",$text);
   
   $patt="/(<\/?)(\w+)([^>]*>)/e";
   $test1="这个文本有<b>粗体</b>和<u>带有下划线</u>以及<i>斜体</i>还有<font color='red' size='7'>带有颜色和字体大小</font>的标记";
   echo "<br>";
   echo preg_replace($patt,"'\\1'.strtoupper('\\2').'\\3'",$test1);
   
   $str1="LAMP是目前最流行的web开发平台:<br>
         LAMP是b/s构架软件开发的黄金组合:<br>
         LAMP每个成员都是开源软件:<br>
         lampbroder是LAMP技术社区:<br>";
         echo str_replace("LAMP","Linux+Apache+MySQL+pHP",$str1,$count);
         echo"区分大小写时共替换了".$count."次<br>";
         echo str_ireplace("LAMP","Linux+Apache+MySQL+pHP",$str1,$count);
         echo"不区分大小写时共替换了".$count."次<br>";
?>

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值