《PHP編程:PHP中一些可以替代正則表達式函數的字符串操作函數》要點:
本文介紹了PHP編程:PHP中一些可以替代正則表達式函數的字符串操作函數,希望對您有用。如果有疑問,可以聯系我們。
0x01:根據預定義的字符對字符串進行詞法分析PHP實戰
代碼如下:
<?php
/*
?* 在處理大量信息時,正則表達式函數會使速度大幅減慢.應當在需要使用正則表達式解析比較復雜的字符串時才能使用這些函數.如果要解析簡單的表達式,還可以采用很多可以顯著加快處理過程的預定義函數.
?*/
/*
?* 根據預定義的字符對字符串進行詞法分析
?* strtok()函數根據預定義的字符列表來解析字符串.其形式為:
?* string strtok(string str,string tokens)
?* strtok()函數,必須連續調用這個函數,才能完全的對一個字符串進行詞法分析;每次調用該函數只是對字符串的下一部分做詞法分析.但是,str參數只需要指定一次,因為函數會跟蹤str中的位置,知道完全對str完成了詞法分析,或者指定了心得str參數.
?* 如下面的例子所示:
?*/
$info="lv chen yang|Hello:world&757104454@qq.com";
//定義界定符,包含(|)(:)( )(&)
$tokens="|:& ";
$tokened=strtok($info, $tokens);
while ($tokened)
{
?echo "Element:$tokened<br/>";
?//連續調用strtok()函數,完成對整個字符串的詞法分析
?$tokened=strtok($tokens);
}
?>
PHP實戰
0x02:根據預定義的定界符分解字符串PHP實戰
代碼如下:
<?php
/*
?* 根據預定義的定界符分解字符串:explode()函數
?* 次函數將字符串str分成子串數組,其形式為:
?* array explode(string separator,string str [, int limit])
?* 原字符串被根據separator指定的字符串分割為不同的元素.元素的數量可以通過可選的參數limit來限制.可以結合explode()/sizeof()和strip_tags()來確定給定文本塊中單詞的總數
?* 如下所示:
?*/
$summary="
???In the latest installment of the ongoing Developer.com PHP series.
???I discuss the many improvements and addtions to
???<a href=\"http:www.php.com\">PHP</a> object-oriented architecture.
???";
echo "<br/>";
$words=explode(' ', strip_tags($summary));
echo "This sentence's lenght is:".sizeof($words);
/*
?* explode()函數始終比preg_split、spilt()和spliti()快得多.因此,在不需要使用正則表達式時,必定要使用這個函數.
?*/
?>
0x03:將數組轉換成字符串PHP實戰
代碼如下:
<?php
/*
?* 將數組轉換成字符串
?* explode()函數可以根據界定字符將字符串轉換為相應的數組,但是可以通過implode()函數將數組轉換為規定的界定字符為邊界的字符串
?* 其形式為:
?* string implode(string delimiter,array pieces)
?* 如下所示:
?*/
$citys=array("Chengdu","Chongqing","Beijing","Shanghai","Guangzhou");
$citystring=implode("|", $citys);
echo $citystring;
?>
0x04:解析復雜的字符串PHP實戰
代碼如下:
<?php
/*
?* 解析復雜的字符串
?* strpos()函數在字符串中以區分大小寫的方式找到substr第一次出現的位置,其形式為
?* int strpos(string str,string substr [,int offset])
?* 可選參數offset指定開始搜索的位置.如果substr不在str中,則strpos()返回False.可選參數確定strpos()從哪里開始搜索.
?* 以下例子將確定第一次拜訪index.html的時間戳:
?*/
$substr="index.html";
$log=<<<logfile
192.168.1.1:/www/htdocs/index.html:[2013/06/26:13:25:10]
192.168.1.2:/www/htdocs/index.html:[2013/06/26:13:27:16]
192.168.1.3:/www/htdocs/index.html:[2013/06/26:13:28:45]
logfile;
echo "<br/>";
//$substr在log中首次出現的位置是什么
$pos=strpos($log, $substr);
//查找行結束的數值位置
$pos1=strpos($log,"\n",$pos);
//計算時間戳的開始
$pos=$pos+strlen($substr)+1;
//檢索時間戳
$timestamp=substr($log, $pos,$pos1-$pos);
echo "The file index.html was first accessed on: $timestamp<br/>";
/*
?* 函數stripos()和函數strpos()函數用法相同,唯一的區別是stripos()不區分大小寫.
?*/
?>
0x05:找到字符串最后一次出現的位置PHP實戰
代碼如下:
<?php
/*
?* 找到字符串中最后一次出現的位置
?* strrpos()函數搜索字符串的最后出現的位置,返回它的位置(數值序號)其形式為:
?* int strrpos(string str,char substr [,offset])
?* 可選參數offset確定strrpos()函數的開始搜索位置.加入希望縮短冗長的新聞總結,
?* 截取總結中的某些部門,并用省略號代替所截去的部門.然而,并非簡單的將總結明確的剪為所需的長度,
?* 你可能希望以一種對用戶友好的方式進行剪切,截取到與階段長度最接近的單詞末尾.
?* 如下例子所示
?*/
$limit=100;
$summary="In the latest installment of the ongoing Developer.com PHP series.
???I discuss the many improvements and addtions to
???<a href=\"http:www.php.com\">PHP</a> object-oriented architecture. ";
if(strlen($summary)>$limit)
?$summary=substr($summary, 0,strrpos(substr($summary, 0,$limit)," "))."...";
echo? $summary;
?>
0x06:用另外一個字符串替換字符串的所有實例PHP實戰
代碼如下:
<?php
/*
?* 用另外一個字符串替換字符串的所有實例
?* str_replace()函數以區分大小寫的方式用另外一個字符串奇幻某個字符串的所有實例.其形式為:
?* mixed str_replace(string occurrence, mixed replacement, mixed str [,int count])
?* 如果str中沒有找到occurrence,則str保持不變,如果定義了可選參數count,則只替換str中count個currence.
?* 此函數很適合對自動獲取電子郵箱地址的程序暗藏電子右鍵地址,如下所示:
?*/
$email="lvchenyang@live.cn";
$email=str_replace("@", "(at)", $email);
echo "<br/>".$email;
?>
0x07:獲取字符串的一部分PHP實戰
代碼如下:
<?php
/*
?* 獲取字符串的一部分
?* strstr()函數返回字符串中從預定義的字符串的第一個出現開始的剩余部分(包含occurrence這個字符串).其形式為:
?* string strstr(string str,string occurrence[,bool fefore_needle])
?* 可選參數before_needle會改變strstr()的行為,使函數返回字符串在第一個出先之前的部分.
?* 下面的例子是獲取右鍵中的域名,結合ltrim()函數
?*/
$url="lvchenyang@live.cn";
echo "<br/>".ltrim(strstr($url, "@"),"@");
?>
0x08:根據預定義的便宜返回字符串的一部分PHP實戰
代碼如下:
<?php
/*
?* substr()函數返回字符串中位于start和start+length之間的部門,其形式為:
?* string substr(string str,int start [,int length])
?* 如果沒有指定的可選參數,則返回從start到str末尾的字符串
?* 如下所示
?*/
$str="lvchenyang";
echo "<br/>".substr($str, 2,4);
//output: chen
?>
0x09:確定字符串出現的頻率PHP實戰
代碼如下:
<?php
/*
?* 確定字符串出現的頻率
?* substr_count()返回一個字符串在另外一個字符串中出現的次數.其形式為:
?* int substr_count(string str,string substring [,int offset [,int length]])
?* 可選參數offset和length指定字符串廉價(從廉價處開始嘗試匹配字符串)和字符串長度(從廉價開始搜索的長度)
?* 下面的例子確定了每個單詞在這個sentence中出現的次數
?*/
$talk=<<<talk
I am acertain that we could dominate mindshare in this space with
our new product, extablishing a true synergy beteen the marketing
and product development teams. We'll own this space in thress months.
talk;
echo "<br/>";
$sentencearray=explode(" ", $talk);
foreach ($sentencearray as $item)
{
?echo "The word <strong>$item</strong> appears(".substr_count($talk, $item).")times<br/>";
}
?>
0x10:用另一個字符串替換一個字符串的一部分PHP實戰
代碼如下:
<?php
/*
?* 用另外一個字符串替換一個字符串的一部門
?* substr_replace()函數將字符串中的一部門用另一個字符串替換,替換從指定的start位置開始,知道start+length位置結束.
?* 其形式為:
?* stringsubstr_replace(string str,string repalcement,int start和length的值.
?* 如下所示,替換電話號碼中間4位
?*/
$phonenum="15926841384";
echo "<br/>".substr_replace($phonenum, "****", 3,4);
?>
維易PHP培訓學院每天發布《PHP編程:PHP中一些可以替代正則表達式函數的字符串操作函數》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/14030.html