|
|
楼主 |
发表于 2013-11-20 15:15:50
|
显示全部楼层
Some useful PHP Keywords and their use
preg_split(PHP 3>= 3.0.9, PHP 4 )preg_split -- Split string by a regular expressionDescriptionarray preg_split ( string pattern, string subject [, int limit [, int flags]])Returns an array containing substrings of subject split along boundaries matched by pattern.If limit is specified, then only substrings up to limit are returned, and if limit is -1, itactually means "no limit", which is useful for specifying the flags.flags can be any combination of the following flags (combined with bitwise | operator) REG_SPLIT_NO_EMPTY If this flag is set, only non-empty pieces will be returned by preg_split().PREG_SPLIT_DELIM_CAPTURE If this flag is set, parenthesized expression in the delimiter pattern will be captured and returned as well. This flag was added for 4.0.5.PREG_SPLIT_OFFSET_CAPTURE If this flag is set, for every occuring match the appendant string offset will also be returned. Note that this changes the return value in an array where every element is an array consisting of the matched string at offset 0 and it's string offset into subject at offset 1. This flag is available since PHP 4.3.0 .Example 1. preg_split() example : Get the parts of a search string- <?php
- // split the phrase by any number of commas or space characters,
- // which include " ", \r, \t, \n and \f
- $keywords = preg_split ("/[\s,]+/", "hypertext language, programming");
- ?>
复制代码
Example 2. Splitting a string into component characters- <?php
- $str = 'string';
- $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
- print_r($chars);
- ?>
复制代码
Example 3. Splitting a string into matches and their offsets- <?php
- $str = 'hypertext language programming';
- $chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
- print_r($chars);
- ?>
复制代码
will yield:Array
(
[0] => Array
(
[0] => hypertext
[1] => 0
)
[1] => Array
(
[0] => language
[1] => 10
)
[2] => Array
(
[0] => programming
[1] => 19
)
)
Note: Parameter flags was added in PHP 4 Beta 3.
preg_match(PHP 3>= 3.0.9, PHP 4 )
preg_match -- Perform a regular expression match
Description
int preg_match ( string pattern, string subject [, array matches [, int flags]])
Searches subject for a match to the regular expression given in pattern.
If matches is provided, then it is filled with the results of search. $matches[0] will
contain the text that matched the full pattern, $matches[1] will have the text that matched
the first captured parenthesized subpattern, and so on.
flags can be the following flag:
PREG_OFFSET_CAPTURE
If this flag is set, for every occuring match the appendant string offset will also
be returned. Note that this changes the return value in an array where every element
is an array consisting of the matched string at offset 0 and it's string offset into
subject at offset 1. This flag is available since PHP 4.3.0 .
The flags parameter is available since PHP 4.3.0 .
preg_match() returns the number of times pattern matches. That will be either 0 times
(no match) or 1 time because preg_match() will stop searching after the first match.
preg_match_all() on the contrary will continue until it reaches the end of subject.
preg_match() returns FALSE if an error occured.
Tip: Do not use preg_match() if you only want to check if one string is contained
in another string. Use strpos() or strstr() instead as they will be faster.
Example 1. Find the string of text "php"- <?php
- // The "i" after the pattern delimiter indicates a case-insensitive search
- if (preg_match ("/php/i", "PHP is the web scripting language of choice.")) {
- print "A match was found.";
- } else {
- print "A match was not found.";
- }
- ?>
复制代码
Example 2. Find the word "web"- <?php
- /* The \b in the pattern indicates a word boundary, so only the distinct
- * word "web" is matched, and not a word partial like "webbing" or "cobweb" */
- if (preg_match ("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
- print "A match was found.";
- } else {
- print "A match was not found.";
- }
- if (preg_match ("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
- print "A match was found.";
- } else {
- print "A match was not found.";
- }
- ?>
复制代码
Example 3. Getting the domain name out of a URL- <?php
- // get host name from URL
- preg_match("/^(http:\/\/)?([^\/]+)/i",
- "http://www.php.net/index.html", $matches);
- $host = $matches[2];
- // get last two segments of host name
- preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
- echo "domain name is: {$matches[0]}\n";
- ?>
复制代码
This example will produce:domain name is: php.net
|
|