午夜久久久久久久久久一区二区_欧美日韩一区二区三区免费看 _欧美国产在线视频_亚洲影院一区

 
深圳網站優化排名

將想法與焦點和您一起共享

深圳網站建設設計 深圳網站優化排名 深圳網站設計制作欣賞

網站制作開始使用PHP正則表達式

2017-07-28  閱讀: 深圳網站建設設計

網站制作開始使用PHP正則表達式

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

正則表達式是從UNIX系統,其中設計了一個程序,調用grep,幫助用戶處理字符串和文本操作。通過遵循幾個基本規則,可以創建非常復雜的搜索模式。

舉個例子,假設你被賦予了檢查電子郵件或電話號碼是否正確的任務。使用一些簡單的命令,由于正則表達式,這些問題可以很容易地得到解決。語法一開始并不總是那么簡單,但是一旦你學會了它,你就會意識到你可以很容易地完成相當復雜的搜索,只需輸入幾個字符,你就可以從不同的角度處理問題。

網站制作開始使用PHP正則表達式

2。兼容正則表達式庫

PHP實現了相當多的正則表達式功能,使用不同的分析引擎。有兩個主要的PHP解析器。一個叫POSIX和PCRE兼容正則表達式或其他。
POSIX的PHP函數的前綴是ereg_。自發布的PHP 5.3這臺發動機是過時的,但讓我們更優更快PCRE引擎一看。
在PHP PCRE函數一開始preg_如preg_match或preg_replace。您可以在PHP文檔中讀取完整的函數列表。


3.基本語法
要使用正則表達式首先需要學習語法。該語法由一系列字母、數字、點、連字符和特殊標志,我們可以一起使用不同的括號。
在PHP中,每個正則表達式模式都被定義為使用Perl格式的字符串。在Perl,一個正則表達式模式是寫在斜杠,如/你好/。在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

除了操作符之外,還有正則表達式修飾符,它可以全局改變搜索模式的行為。

正則表達式修飾符放在模式,這樣/你好/我和他們由單字母如我這標志著一個模式不區分大小寫或X忽略空白字符。要獲得修飾符的完整列表,請訪問PHP的在線文檔。

正則表達式的真正力量依賴于合并這些操作符和修飾符,因此創建相當復雜的搜索模式。

網站制作開始使用PHP正則表達式
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);

請注意,正則表達式是區分大小寫的,上面的模式將不匹配hello這個單詞。如果我們希望我們的模式不區分大小寫,我們應該應用下面的修飾符:

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

請注意在斜杠后面的模式后面的字符i。

現在讓我們來研究一個更復雜的搜索模式。如果我們要檢查字符串中的前五個字符是alpha數字字符怎么辦?。

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

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

從A到Z的字母A-Z,其次是相同的除了小寫字符的所有字符,這是很重要的,因為正則表達式是大小寫敏感。我想你會明白自己什么0-9的手段。

{ 5 }只是告訴正則表達式分析器的準確計數五字。如果我們將六替換為五,解析器將不匹配任何東西,因為在我們的測試字符串中,hello是五個字符長,后面是空格字符,在我們的例子中是不計數的。

此外,這個正則表達式可以優化為以下形式:

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

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


6。有用的正則表達式函數

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

驗證電子郵件。這個函數將驗證給定的電子郵件地址字符串,以確定它是否有正確的格式。

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。正則表達式的小抄
因為小抄現在是涼的,下面你可以找到一個小抄,可以運行通過,很快你忘了什么東西。

將文章分享到..
午夜久久久久久久久久一区二区_欧美日韩一区二区三区免费看 _欧美国产在线视频_亚洲影院一区
久久精品72免费观看| 欧美二区在线看| 亚洲三级电影全部在线观看高清| 国产欧美日韩亚洲精品| 欧美日韩成人综合在线一区二区 | 欧美在线免费| 亚洲综合三区| 亚洲尤物视频网| 亚洲一二三级电影| 亚洲天堂男人| 亚洲综合成人在线| 亚洲一级黄色av| 亚洲天堂偷拍| 亚洲女同在线| 小黄鸭精品密入口导航| 亚洲欧美综合国产精品一区| 亚洲伊人一本大道中文字幕| 亚洲综合第一| 欧美伊人久久久久久久久影院 | 91久久精品一区| 亚洲黄色一区二区三区| 亚洲人成绝费网站色www| 亚洲国产日本| 亚洲精品一级| 国产精品99久久久久久有的能看| 亚洲天堂免费观看| 性欧美xxxx视频在线观看| 欧美在线在线| 久久人人97超碰国产公开结果| 另类图片国产| 欧美精品免费视频| 国产精品99一区| 国产精品永久在线| 国内精品写真在线观看| 亚洲国产精品va在看黑人| 亚洲精品国产精品国自产观看| 99riav国产精品| 亚洲欧美日韩一区在线| 久久亚洲精品一区| 久久久国产91| 欧美成人一区二免费视频软件| 欧美精品福利| 国产精品久久久久久一区二区三区| 国产精品日韩电影| 精品99一区二区三区| 亚洲人成高清| 亚洲天堂成人在线视频| 欧美一区二区三区四区在线 | 国产精品久久久久久久久免费桃花| 国产精品五区| 在线成人欧美| 99re8这里有精品热视频免费| 亚洲男人av电影| 久久精品123| 欧美另类视频在线| 国产精品自在欧美一区| 在线欧美小视频| 在线一区欧美| 久久国内精品视频| 欧美精品久久久久a| 国产伦精品一区| 亚洲国产91| 亚洲欧美激情四射在线日| 久热精品视频在线免费观看| 欧美日本亚洲韩国国产| 国产日本欧美一区二区三区| 亚洲黄色成人网| 亚洲欧美日韩国产一区二区| 牛牛国产精品| 国产日韩精品一区观看| 亚洲精品美女91| 性欧美精品高清| 欧美激情一二区| 国产一区二区精品久久91| 亚洲精品一区二| 久久成人国产精品| 欧美日韩精品综合| 在线不卡欧美| 午夜在线播放视频欧美| 欧美日韩激情网| 在线观看欧美日韩| 午夜视频一区在线观看| 欧美精品三级在线观看| 狠狠色综合色综合网络| 亚洲一区二区精品视频| 狼人天天伊人久久| 国产欧美二区| 中文av字幕一区| 欧美大片免费| 国产永久精品大片wwwapp| 亚洲小视频在线观看| 欧美高清成人| 好吊视频一区二区三区四区| 亚洲在线观看| 欧美精品三级| 在线欧美日韩精品| 久久国内精品视频| 国产精品视频免费观看www| 亚洲伦理网站| 免费在线成人av| 狠狠色狠狠色综合日日小说| 欧美一区亚洲| 国产精品美女久久久免费| 亚洲免费观看在线视频| 麻豆精品网站| 红桃av永久久久| 欧美一区二区三区男人的天堂| 欧美性猛交xxxx乱大交蜜桃| 亚洲精选成人| 欧美www在线| 亚洲电影在线看| 久久久久国色av免费观看性色| 国产精品成人av性教育| 日韩亚洲欧美在线观看| 欧美福利一区二区| 亚洲激情一区二区三区| 久久综合九色九九| 激情综合激情| 久久久久在线观看| 国产主播精品在线| 欧美一区视频在线| 国产三区精品| 久久国产免费| 国语自产精品视频在线看| 久久国产乱子精品免费女 | 欧美色综合天天久久综合精品| 亚洲精品网站在线播放gif| 欧美99久久| 亚洲欧洲一区| 欧美激情免费在线| 亚洲六月丁香色婷婷综合久久| 欧美日本精品| 一区二区三区久久网| 欧美日韩中文另类| 一区二区三区欧美在线| 欧美午夜精品| 午夜精品久久久99热福利| 国产免费成人| 久久国产精品99国产| 国内免费精品永久在线视频| 久久午夜精品一区二区| 亚洲福利在线视频| 欧美高清在线视频| 99在线精品视频在线观看| 欧美性理论片在线观看片免费| 午夜精品福利电影| 国产一区二区三区的电影| 久久人体大胆视频| 91久久线看在观草草青青| 欧美麻豆久久久久久中文| 亚洲一区二区三区精品在线| 国产色综合网| 卡一卡二国产精品| 亚洲精品专区| 国产精品另类一区| 久久9热精品视频| 在线欧美日韩| 欧美日韩精品一区二区| 亚洲免费综合| 韩国一区二区在线观看| 免费视频久久| 一区二区三区高清在线观看| 国产精品综合| 免费观看久久久4p| 亚洲天堂激情| 激情91久久| 欧美日韩在线播放三区四区| 午夜日韩在线观看| 亚洲黄色免费网站| 国产精品一区二区久久国产| 久久综合久久综合这里只有精品| 亚洲狼人综合| 国产日韩精品一区观看| 欧美成人午夜77777| 亚洲女人小视频在线观看| 精品成人在线视频| 欧美三级午夜理伦三级中文幕 | 亚洲最新在线| 国产色婷婷国产综合在线理论片a| 久久综合伊人77777| 亚洲视频欧美视频| 国产又爽又黄的激情精品视频| 欧美激情bt| 欧美一级片在线播放| 亚洲精品视频一区| 国产视频精品xxxx| 欧美美女操人视频| 久久久不卡网国产精品一区| 9色精品在线| 狠狠色狠狠色综合人人| 欧美日韩一区二区精品| 久久久久久一区| 亚洲视频精品| 亚洲国产精品va在线看黑人动漫| 国产精品久久福利| 欧美aⅴ一区二区三区视频| 亚洲欧美在线磁力| 亚洲精品在线观| 国产一区二区三区成人欧美日韩在线观看| 欧美激情视频一区二区三区不卡|