PHP中一些可以替代Regex函數的字串操作函數,phpRegex_PHP教程

來源:互聯網
上載者:User

PHP中一些可以替代Regex函數的字串操作函數,phpRegex


0x01:根據預定義的字元對字串進行詞法分析

複製代碼 代碼如下:
<?php
/*
* 在處理大量資訊時,Regex函數會使速度大幅減慢。應當在需要使用Regex解析比較複雜的字串時才能使用這些函數。如果要解析簡單的運算式,還可以採用很多可以顯著加快處理過程的預定義函數。
*/

/*
* 根據預定義的字元對字串進行詞法分析
* 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
";
//連續調用strtok()函數,完成對整個字串的詞法分析
$tokened=strtok($tokens);
}
?>

0x02:根據預定義的定界符分解字串

複製代碼 代碼如下:
<?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
PHP object-oriented architecture.
";
echo "
";
$words=explode(' ', strip_tags($summary));
echo "This sentence's lenght is:".sizeof($words);
/*
* explode()函數始終比preg_split、spilt()和spliti()快得多。因此,在不需要使用Regex時,一定要使用這個函數。
*/
?>

0x03:將數群組轉換成字串

複製代碼 代碼如下:
<?php
/*
* 將數群組轉換成字串
* explode()函數可以根據界定字元將字串轉換為相應的數組,但是可以通過implode()函數將數群組轉換為規定的界定字元為界限的字串
* 其形式為:
* string implode(string delimiter,array pieces)
* 如下所示:
*/
$citys=array("Chengdu","Chongqing","Beijing","Shanghai","Guangzhou");
$citystring=implode("|", $citys);
echo $citystring;
?>

0x04:解析複雜的字串

複製代碼 代碼如下:
<?php
/*
* 解析複雜的字串
* strpos()函數在字串中以區分大小寫方式找到substr第一次出現的位置,其形式為
* int strpos(string str,string substr [,int offset])
* 選擇性參數offset指定開始搜尋的位置。如果substr不在str中,則strpos()返回False。選擇性參數確定strpos()從哪裡開始搜尋。
* 以下例子將確定第一次訪問index.html的時間戳記:
*/
$substr="index.html";
$log=<<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 "
";
//$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
";
/*
* 函數stripos()和函數strpos()函數用法相同,唯一的區別是stripos()不區分大小寫。
*/
?>

0x05:找到字串最後一次出現的位置

複製代碼 代碼如下:
<?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
PHP object-oriented architecture. ";
if(strlen($summary)>$limit)
$summary=substr($summary, 0,strrpos(substr($summary, 0,$limit)," "))."...";
echo $summary;
?>

0x06:用另外一個字串替換字串的所有執行個體

複製代碼 代碼如下:
<?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 "
".$email;
?>

0x07:擷取字串的一部分

複製代碼 代碼如下:
<?php
/*
* 擷取字串的一部分
* strstr()函數返回字串中從預定義的字串的第一個出現開始的剩餘部分(包括occurrence這個字串)。其形式為:
* string strstr(string str,string occurrence[,bool fefore_needle])
* 選擇性參數before_needle會改變strstr()的行為,使函數返回字串在第一個出先之前的部分。
* 下面的例子是擷取右鍵中的網域名稱,結合ltrim()函數
*/
$url="lvchenyang@live.cn";
echo "
".ltrim(strstr($url, "@"),"@");
?>

0x08:根據預定義的便宜返回字串的一部分

複製代碼 代碼如下:
<?php
/*
* substr()函數返回字串中位於start和start+length之間的部分,其形式為:
* string substr(string str,int start [,int length])
* 如果沒有指定的選擇性參數,則返回從start到str末尾的字串
* 如下所示
*/
$str="lvchenyang";
echo "
".substr($str, 2,4);
//output: chen
?>

0x09:確定字串出現的頻率

複製代碼 代碼如下:
<?php
/*
* 確定字串出現的頻率
* substr_count()返回一個字串在另外一個字串中出現的次數。其形式為:
* int substr_count(string str,string substring [,int offset [,int length]])
* 選擇性參數offset和length指定字串便宜(從便宜處開始嘗試匹配字串)和字串長度(從便宜開始搜尋的長度)
* 下面的例子確定了每個單詞在這個sentence中出現的次數
*/
$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 "
";
$sentencearray=explode(" ", $talk);
foreach ($sentencearray as $item)
{
echo "The word $item appears(".substr_count($talk, $item).")times
";
}
?>

0x10:用另一個字串替換一個字串的一部分

複製代碼 代碼如下:
<?php
/*
* 用另外一個字串替換一個字串的一部分
* substr_replace()函數將字串中的一部分用另一個字串替換,替換從指定的start位置開始,知道start+length位置結束。
* 其形式為:
* stringsubstr_replace(string str,string repalcement,int start和length的值。
* 如下所示,替換電話號碼中間4位
*/
$phonenum="15926841384";
echo "
".substr_replace($phonenum, "****", 3,4);
?>

http://www.bkjia.com/PHPjc/912669.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/912669.htmlTechArticlePHP中一些可以替代Regex函數的字串操作函數,phpRegex 0x01:根據預定義的字元對字串進行詞法分析 複製代碼 代碼如下: ph...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.