網(wǎng)站制作開(kāi)始使用PHP正則表達(dá)式

2023-07-11    分類: 網(wǎng)站建設(shè)

網(wǎng)站制作開(kāi)始使用PHP正則表達(dá)式成都網(wǎng)站建設(shè)

1。正則表達(dá)式是什么?

正則表達(dá)式的主要目的,也被稱為正則表達(dá)式或regexp,是有效地搜索模式在一個(gè)給定的文本。這些搜索模式是使用正則表達(dá)式解析器理解的特殊格式編寫的。

正則表達(dá)式是從UNIX系統(tǒng),其中設(shè)計(jì)了一個(gè)程序,調(diào)用grep,幫助用戶處理字符串和文本操作。通過(guò)遵循幾個(gè)基本規(guī)則,可以創(chuàng)建非常復(fù)雜的搜索模式。

舉個(gè)例子,假設(shè)你被賦予了檢查電子郵件或電話號(hào)碼是否正確的任務(wù)。使用一些簡(jiǎn)單的命令,由于正則表達(dá)式,這些問(wèn)題可以很容易地得到解決。語(yǔ)法一開(kāi)始并不總是那么簡(jiǎn)單,但是一旦你學(xué)會(huì)了它,你就會(huì)意識(shí)到你可以很容易地完成相當(dāng)復(fù)雜的搜索,只需輸入幾個(gè)字符,你就可以從不同的角度處理問(wèn)題。

網(wǎng)站制作開(kāi)始使用PHP正則表達(dá)式

2。兼容正則表達(dá)式庫(kù)

PHP實(shí)現(xiàn)了相當(dāng)多的正則表達(dá)式功能,使用不同的分析引擎。有兩個(gè)主要的PHP解析器。一個(gè)叫POSIX和PCRE兼容正則表達(dá)式或其他。

POSIX的PHP函數(shù)的前綴是ereg_。自發(fā)布的PHP 5.3這臺(tái)發(fā)動(dòng)機(jī)是過(guò)時(shí)的,但讓我們更優(yōu)更快PCRE引擎一看。

在PHP PCRE函數(shù)一開(kāi)始preg_如preg_match或preg_replace。您可以在PHP文檔中讀取完整的函數(shù)列表。

3.基本語(yǔ)法

要使用正則表達(dá)式首先需要學(xué)習(xí)語(yǔ)法。該語(yǔ)法由一系列字母、數(shù)字、點(diǎn)、連字符和特殊標(biāo)志,我們可以一起使用不同的括號(hào)。

在PHP中,每個(gè)正則表達(dá)式模式都被定義為使用Perl格式的字符串。在Perl,一個(gè)正則表達(dá)式模式是寫在斜杠,如/你好/。在PHP中這將成為一個(gè)字符串,“你好”。

Now,let’s have a look at some operators,the basic building blocks of regular expressions

Operator Description

^The circumflex symbol marks the beginning of a pattern,although in some cases it can be omitted

$Same as with the circumflex symbol,the dollar sign marks the end of a search pattern

.The period matches any single character

?It will match the preceding pattern zero or one times

+It will match the preceding pattern one or more times

*It will match the preceding pattern zero or more times

|Boolean OR

–Matches a range of elements

()Groups a different pattern elements together

[]Matches any single character between the square brackets

{min,max}It is used to match exact character counts

d Matches any single digit

D Matches any single non digit caharcter

w Matches any alpha numeric character including underscore(_)

W Matches any non alpha numeric character excluding the underscore character

s Matches whitespace character

As an addition in PHP the forward slash character is escaped using the simple slash.Example:‘/he/llo/’

To have a brief understanding how these operators are used,let’s have a look at a few examples:

Example Description

‘/hello/’It will match the word hello

‘/^hello/’It will match hello at the start of a string.Possible matches are hello or helloworld,but not worldhello

‘/hello$/’It will match hello at the end of a string.

‘/he.o/’It will match any character between he and o.Possible matches are helo or heyo,but not hello

‘/he?llo/’It will match either llo or hello

‘/hello+/’It will match hello on or more time.E.g.hello or hellohello

‘/he*llo/’Matches llo,hello or hehello,but not hellooo

‘/hello|world/’It will either match the word hello or world

‘/(A-Z)/’Using it with the hyphen character,this pattern will match every uppercase character from A to Z.E.g.A,B,C…

‘/[abc]/’It will match any single character a,b or c

‘/abc{1}/’Matches precisely one c character after the characters ab.E.g.matches abc,but not abcc

‘/abc{1,}/’Matches one or more c character after the characters ab.E.g.matches abc or abcc

‘/abc{2,4}/’Matches between two and four c character after the characters ab.E.g.matches abcc,abccc or abcccc,but not abc

除了操作符之外,還有正則表達(dá)式修飾符,它可以全局改變搜索模式的行為。成都網(wǎng)站建設(shè)

正則表達(dá)式修飾符放在模式,這樣/你好/我和他們由單字母如我這標(biāo)志著一個(gè)模式不區(qū)分大小寫或X忽略空白字符。要獲得修飾符的完整列表,請(qǐng)?jiān)L問(wèn)PHP的在線文檔。

正則表達(dá)式的真正力量依賴于合并這些操作符和修飾符,因此創(chuàng)建相當(dāng)復(fù)雜的搜索模式。

網(wǎng)站制作開(kāi)始使用PHP正則表達(dá)式

4.Using Regex in PHP

In PHP we have a total of nine PCRE functions which we can use.Here’s the list:

preg_filter–performs a regular expression search and replace

preg_grep–returns array entries that match a pattern

preg_last_error–returns the error code of the last PCRE regex execution

preg_match–perform a regular expression match

preg_match_all–perform a global regular expression match

preg_quote–quote regular expression characters

preg_replace–perform a regular expression search and replace

preg_replace_callback–perform a regular expression search and replace using a callback

preg_split–split string by a regular expression

The two most commonly used functions are preg_match and preg_replace.

Let’s begin by creating a test string on which we will perform our regular expression searches.The classical hello world should do it.

$test_string='hello world';

If we simply want to search for the word hello or world then the search pattern would look something like this:

preg_match('/hello/',$test_string);

preg_match('/world/',$test_string);

如果我們想看看字符串是否以hello開(kāi)頭,我們只需在搜索模式的開(kāi)頭放置這個(gè)字符:

preg_match('/^hello/',$test_string);

請(qǐng)注意,正則表達(dá)式是區(qū)分大小寫的,上面的模式將不匹配hello這個(gè)單詞。如果我們希望我們的模式不區(qū)分大小寫,我們應(yīng)該應(yīng)用下面的修飾符:

preg_match('/^hello/i',$test_string);

請(qǐng)注意在斜杠后面的模式后面的字符i。

現(xiàn)在讓我們來(lái)研究一個(gè)更復(fù)雜的搜索模式。如果我們要檢查字符串中的前五個(gè)字符是alpha數(shù)字字符怎么辦?。

preg_match('/^[A-Za-z0-9]{5}/',$test_string);

讓我們來(lái)剖析這個(gè)搜索模式。首先,采用插入字符(^)我們指定的字符串必須以一個(gè)字母數(shù)字字符。這是由[就]指定。

從A到Z的字母A-Z,其次是相同的除了小寫字符的所有字符,這是很重要的,因?yàn)檎齽t表達(dá)式是大小寫敏感。我想你會(huì)明白自己什么0-9的手段。

{5}只是告訴正則表達(dá)式分析器的準(zhǔn)確計(jì)數(shù)五字。如果我們將六替換為五,解析器將不匹配任何東西,因?yàn)樵谖覀兊臏y(cè)試字符串中,hello是五個(gè)字符長(zhǎng),后面是空格字符,在我們的例子中是不計(jì)數(shù)的。

此外,這個(gè)正則表達(dá)式可以優(yōu)化為以下形式:

preg_match('/^w{5}/',$test_string);

w specifies any alpha numeric characters plus the underscore character(_).

6。有用的正則表達(dá)式函數(shù)

下面是一些使用正則表達(dá)式的PHP函數(shù),您可以在日常中使用它們。

驗(yàn)證電子郵件。這個(gè)函數(shù)將驗(yàn)證給定的電子郵件地址字符串,以確定它是否有正確的格式。

function validate_email($email_address)

{

if(!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])* ([a-zA-Z0-9_-])+

([a-zA-Z0-9._-]+)+$/",$email_address))

{

return false;

}

return true;

}

Validate a URL

function validate_url($url)

{

return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?

(/.*)?$|i',$url);

}

Remove repeated words.I often found repeated words in a text,such as this this.This handy function will remove such duplicate words.

function remove_duplicate_word($text)

{

return preg_replace("/s(w+s)1/i","$1",$text);

}

Validate alpha numeric,dashes,underscores and spaces

function validate_alpha($text)

{

return preg_match("/^[A-Za-z0-9_-]+$/",$text);

}

Validate US ZIP codes

function validate_zip($zip_code)

{

return preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zip_code);

}

7。正則表達(dá)式的小抄

因?yàn)樾〕F(xiàn)在是涼的,下面你可以找到一個(gè)小抄,可以運(yùn)行通過(guò),很快你忘了什么東西。

以上信息來(lái)源網(wǎng)絡(luò),本文關(guān)鍵詞:成都網(wǎng)站建設(shè)

文章標(biāo)題:網(wǎng)站制作開(kāi)始使用PHP正則表達(dá)式
標(biāo)題鏈接:http://bm7419.com/news/271797.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)、用戶體驗(yàn)外貿(mào)網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)