PCRE là vượt quá phạm vi của cuốn sách này, nhưng Bảng 5-1 liệt kê các nhân vật phổ biến nhất và bổ được sử dụng trong xây dựng các biểu thức thông thường. Tóm lại, PCRE này tìm kiếm một chuỗi không bắt đầu với một khoảng thời gian, nhưng có chứa ít nhất một giai đoạn tiếp theo là ít nhất hai nhân vật. | Simpo PDF Merge and Split Unregistered Version - http PHP OBJECT-ORIENTED SOLUTIONS of PCRE is beyond the scope of this book but Table 5-1 lists the most common characters and modifiers used in building regular expressions. To summarize this PCRE looks for a string that does not begin with a period but contains at least one period followed by at least two characters. It s a very crude check because it accepts something like .a_ which doesn t resemble a domain name in the slightest. However the idea is to catch simple typing errors rather than to strive to create the perfect PCRE. Creating a PCRE to match a valid domain name is remarkably complex and there s a danger it could be made obsolete by the approval of new top-level domains. Fortunately you don t always need to create your own regular expressions as there are a number of regular expression libraries online. One of the most popular is at http . The URL is a reference to the other common abbreviation for regular expression regex. Use this PCRE with preg_match to find a match like this domainOK preg_match A A. . w 2 this- _urlParts host The preg_match function requires two arguments a PCRE and the string that you want to search. As you ll see later in this chapter it also takes an optional third argument which captures an array of matches. If the value of the host element of _urlParts matches the pattern preg_match returns true. If there s no match it returns false. The revised version of checkURL now looks like this protected function checkURL flags FILTER_FLAG_SCHEME_REQUIRED FILTER_FLAG_HOST_REQUIRED urlOK filter_var this- _url FILTER_VALIDATE_URL flags this- _urlParts parse_url this- _url domainOK preg_match A A. . w 2 this- _urlParts host if urlOK this- _urlParts scheme http domainOK throw new Exception this- _url . is not a valid URL PHP 5 supports two types of regex PCRE and Portable Operating System Interface POSIX . Functions that begin with preg_ support PCRE while .