<?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>";
?>