網(wǎng)站制作開始使用PHP正則表達(dá)式
1。正則表達(dá)式是什么?
正則表達(dá)式的主要目的,也被稱為正則表達(dá)式或regexp,是有效地搜索模式在一個給定的文本。這些搜索模式是使用正則表達(dá)式解析器理解的特殊格式編寫的。
正則表達(dá)式是從UNIX系統(tǒng),其中設(shè)計了一個程序,調(diào)用grep,幫助用戶處理字符串和文本操作。通過遵循幾個基本規(guī)則,可以創(chuàng)建非常復(fù)雜的搜索模式。
舉個例子,假設(shè)你被賦予了檢查電子郵件或電話號碼是否正確的任務(wù)。使用一些簡單的命令,由于正則表達(dá)式,這些問題可以很容易地得到解決。語法一開始并不總是那么簡單,但是一旦你學(xué)會了它,你就會意識到你可以很容易地完成相當(dāng)復(fù)雜的搜索,只需輸入幾個字符,你就可以從不同的角度處理問題。
2。兼容正則表達(dá)式庫
PHP實(shí)現(xiàn)了相當(dāng)多的正則表達(dá)式功能,使用不同的分析引擎。有兩個主要的PHP解析器。一個叫POSIX和PCRE兼容正則表達(dá)式或其他。
POSIX的PHP函數(shù)的前綴是ereg_。自發(fā)布的PHP 5.3這臺發(fā)動機(jī)是過時的,但讓我們更優(yōu)更快PCRE引擎一看。
在PHP PCRE函數(shù)一開始preg_如preg_match或preg_replace。您可以在PHP文檔中讀取完整的函數(shù)列表。
3.基本語法
要使用正則表達(dá)式首先需要學(xué)習(xí)語法。該語法由一系列字母、數(shù)字、點(diǎn)、連字符和特殊標(biāo)志,我們可以一起使用不同的括號。
在PHP中,每個正則表達(dá)式模式都被定義為使用Perl格式的字符串。在Perl,一個正則表達(dá)式模式是寫在斜杠,如/你好/。在PHP中這將成為一個字符串,“你好”。
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á)式修飾符,它可以全局改變搜索模式的行為。
正則表達(dá)式修飾符放在模式,這樣/你好/我和他們由單字母如我這標(biāo)志著一個模式不區(qū)分大小寫或X忽略空白字符。要獲得修飾符的完整列表,請訪問PHP的在線文檔。
正則表達(dá)式的真正力量依賴于合并這些操作符和修飾符,因此創(chuàng)建相當(dāng)復(fù)雜的搜索模式。
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開頭,我們只需在搜索模式的開頭放置這個字符:
preg_match('/^hello/', $test_string);
請注意,正則表達(dá)式是區(qū)分大小寫的,上面的模式將不匹配hello這個單詞。如果我們希望我們的模式不區(qū)分大小寫,我們應(yīng)該應(yīng)用下面的修飾符:
preg_match('/^hello/i', $test_string);
請注意在斜杠后面的模式后面的字符i。
現(xiàn)在讓我們來研究一個更復(fù)雜的搜索模式。如果我們要檢查字符串中的前五個字符是alpha數(shù)字字符怎么辦?。
preg_match('/^[A-Za-z0-9]{5}/', $test_string);
讓我們來剖析這個搜索模式。首先,采用插入字符(^)我們指定的字符串必須以一個字母數(shù)字字符。這是由[就]指定。
從A到Z的字母A-Z,其次是相同的除了小寫字符的所有字符,這是很重要的,因?yàn)檎齽t表達(dá)式是大小寫敏感。我想你會明白自己什么0-9的手段。
{ 5 }只是告訴正則表達(dá)式分析器的準(zhǔn)確計數(shù)五字。如果我們將六替換為五,解析器將不匹配任何東西,因?yàn)樵谖覀兊臏y試字符串中,hello是五個字符長,后面是空格字符,在我們的例子中是不計數(shù)的。
此外,這個正則表達(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)證電子郵件。這個函數(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)在是涼的,下面你可以找到一個小抄,可以運(yùn)行通過,很快你忘了什么東西。