Hướng dẫn dùng lookahead trong PHP

Khi chúng ta chạy một đoạn code PHP, có rất nhiều thứ xảy ra sâu bên dưới mà ta không nhìn thấy. Một cách khái quát, bộ thông dịch PHP trải qua 4 giai đoạn khi nó thực thi 1 đoạn code:

  • Lexing

  • Parsing

  • BIên dịch [compilation]

  • Thông dịch [intepretation]

Bài viết này sẽ tập trung vào các giai đoạn này, biểu diễn các output trong mỗi giai đoạn để hiểu bản chất. Cần chú ý rằng một số extension được cài đặt mặc định cùng PHP [ tokenizer, OPcache, …], trong khi đó nhiều extension bắt buộc phải cài đặt và khởi động 1 cách thủ công [ php-ast và VLD ].

Giai đoạn 1: LEXING

Lexing [tokenizing] là quá trình chuyển một đoạn string [mã nguồnPHP] thành 1 chuỗi các token. Một token đơn giản là một định danh gắn với một giá trị. PHP sử dụng re2c để sinh ra các lexer của nó từ file khai báo zend_language_scanner.

An assertion is a test on the characters following or preceding the current matching point that does not actually consume any characters. The simple assertions coded as \b, \B, \A, \Z, \z, ^ and $ are described in escape sequences. More complicated assertions are coded as subpatterns. There are two kinds: those that look ahead of the current position in the subject string, and those that look behind it.

An assertion subpattern is matched in the normal way, except that it does not cause the current matching position to be changed. Lookahead assertions start with [?= for positive assertions and [?! for negative assertions. For example, \w+[?=;] matches a word followed by a semicolon, but does not include the semicolon in the match, and foo[?!bar] matches any occurrence of "foo" that is not followed by "bar". Note that the apparently similar pattern [?!foo]bar does not find an occurrence of "bar" that is preceded by something other than "foo"; it finds any occurrence of "bar" whatsoever, because the assertion [?!foo] is always true when the next three characters are "bar". A lookbehind assertion is needed to achieve this effect.

Lookbehind assertions start with [? The fat cat sat on the mat. [T|t]he[?=\sfat] => The fat cat sat on the mat. 1 /google+\.com/ sẽ coi dấu dấu là dấu chấm theo nghĩa đen, ví dụ: google.com|Biểu diễn thay thế, phép toán 

[T|t]he => The fat cat sat on the mat.
[T|t]he[?=\sfat] => The fat cat sat on the mat.
2x|y sẽ khớp x hoặc y[]Tập hợp ký tự. Phù hợp nếu có bất kỳ ký tự nào trong dấu [] /[abc]/ sẽ khớp với a,b,c[^]Tập hợp ký tự phủ định. Phù hợp nếu không có ký tự nào trong []/[^xyz]/ sẽ khớp nếu loại trừ các ký tự xyz[xyz]Biểu diễn một nhóm ký tự/^[xyz]$/ sẽ khớp với xyz.Khớp với bất kỳ ký tự đơn nào trừ dòng mới/^a.$/ khớp với an, am,…–Khoảng liên tiếp các giá trịx-z tức các giá trị x,y,za-z Khớp với các ký tự thường từ a-z /a-z/ sẽ khớp với các ký tự thường từ a-zA-Z Khớp với các ký tự Hoa từ A-Z /A-Z/ sẽ khớp với các ký tự chữa hoa A-Z0-9 Khớp bất kỳ số nào trong khoảng từ 0 đến 9 /0-9/ sẽ khớp với các ký số

Ký hiệu tắt cho tập hợp

Viết tắtDiễn tả\wChữ,sô, và _, tương đương với: 
[T|t]he => The fat cat sat on the mat.
[T|t]he[?=\sfat] => The fat cat sat on the mat.
3\WNgoài bảng chữ cái, tương đương với: 
[T|t]he => The fat cat sat on the mat.
[T|t]he[?=\sfat] => The fat cat sat on the mat.
4\dCác số: 
[T|t]he => The fat cat sat on the mat.
[T|t]he[?=\sfat] => The fat cat sat on the mat.
5\DKhông phải số: 
[T|t]he => The fat cat sat on the mat.
[T|t]he[?=\sfat] => The fat cat sat on the mat.
6\sLà ký tự trắng, tương đương với: 
[T|t]he => The fat cat sat on the mat.
[T|t]he[?=\sfat] => The fat cat sat on the mat.
7\SKhông phải ký tự trắng: 
[T|t]he => The fat cat sat on the mat.
[T|t]he[?=\sfat] => The fat cat sat on the mat.
8

Các cờ – flags

CờDiễn tảiThiết lập không phân biệt chữ hoa chữ thườnggTìm kiếm toàn chuỗi.mSo khớp trên từng dòng đối với văn bản đa dòng và có sử dụng cặp “^$”


Các cờ này được đưa vào mẫu theo dạng 

[T|t]he => The fat cat sat on the mat.
[T|t]he[?=\sfat] => The fat cat sat on the mat.
9

[T|t]he[?!\sfat] => The fat cat sat on the mat.
0 => The fat cat sat on the mat.

[T|t]he[?!\sfat] => The fat cat sat on the mat.
1 => The fat cat sat on the mat.

[T|t]he[?!\sfat] => The fat cat sat on the mat.
2 => The fat cat sat on the mat.

Biểu thức ?= lookahead

Lookahead 

[T|t]he[?!\sfat] => The fat cat sat on the mat.
3 cho thêm vào để lọc kết quả.

Ký hiệu 

[T|t]he[?!\sfat] => The fat cat sat on the mat.
3. Phần đầu của biểu thức phải được tiếp nối bởi biểu thức 
[T|t]he[?!\sfat] => The fat cat sat on the mat.
5.
Ví dụ 
[T|t]he[?!\sfat] => The fat cat sat on the mat.
6 thì 
[T|t]he[?!\sfat] => The fat cat sat on the mat.
5 là 
[T|t]he[?!\sfat] => The fat cat sat on the mat.
8 – nghĩa là 
[T|t]he[?!\sfat] => The fat cat sat on the mat.
9 hoặc 
[? The fat cat sat on the mat.
0 theo sau là 
[? The fat cat sat on the mat.
1 vậy tìm được 2 kết quả. Nhưng do có biểu thức lookahead, điều này thì kết quả phù hợp là chỉ lấy khi theo sau nó là chuỗi 
[? The fat cat sat on the mat.
2

[T|t]he => The fat cat sat on the mat.
[T|t]he[?=\sfat] => The fat cat sat on the mat.

Biểu thức ?! phủ định lookahead

Ký hiệu là 

[? The fat cat sat on the mat.
3, nghĩa là lấy kết quả mà đi sau nó không có chuỗi 
[T|t]he[?!\sfat] => The fat cat sat on the mat.
5

[T|t]he[?!\sfat] => The fat cat sat on the mat.

Biểu thức [?

Chủ Đề