For php strings and regular expressions, for php regular expressions. Explanation of strings and regular expressions in php, explanation of php Regular Expressions 1. characteristics of string types 1. PHP is a weak language, other data types can be directly applied to strings and regular expressions in php. php regular expressions
I. features of the string type
1. PHP is a weak type language. other data types can be directly applied to string function operations.
<? Php
Echo substr ("123456", 345); // output
Echo substr (123456,2, 4); // output 345
Echo hello; // search for the hello constant first. If no hello constant is found, use "hello" as a string.
?>
2. a string can be used as an "array" and is a collection of characters.
<?php
$str = "www.jb51.net";
echo $str[0];
echo $str[1];
echo $str[2];
?>
But the string is not a real array, and the array function cannot be used. for example, count ($ str) does not return the string length. The PHP engine cannot distinguish characters from arrays to produce ambiguity. Square brackets have been replaced with curly brackets since PHP4.
<? Php
// To ensure backward compatibility, square brackets can still be used
$ Str = www.jb51.net;
Echo $ str {0 };
Echo $ str {1 };
Echo $ str {2 };
?>
3. variable parsing with double quotation marks
In PHP, when a string is defined using double quotation marks or delimiters, the variables in the string are parsed.
<? Php
$ Arr = array ('name' => "dwqs", 'add' => "www.ido321.com ");
Echo "$ arr [name]"; // it can be parsed, But quotation marks cannot be used in square brackets.
// Echo "$ arr ['name']"; error
Echo "{$ arr ['name']}"; // it can be parsed. elements are included in curly brackets. name is also possible without quotation marks.
// Assume that the object $ square exists.
Echo "$ square-> width"; // parses
Echo "$ square-> width00 cent"; // it cannot be parsed and is resolved in curly brackets.
Echo "{$ square-> width} width00 cent"; // can be parsed
?>
II. string output functions
III. common string format functions
PS: most PHP string processing functions do not modify the source string, but return a new string.
IV. regular expressions
Regular expressions describe a string matching mode. In this mode, strings are matched, searched, replaced, and separated in specific functions, it consists of three parts: Atomic, metacharacter, and pattern modifier.
In PHP, there are two regular processing function libraries: PCRE and POSIX. The former is named with the prefix and is compatible with Perl. The latter is named with the prefix ereg. The two functions are similar, but the efficiency of PCRE is slightly higher.
Regular expression processing functions compatible with Perl:
1. Syntax
1.1 delimiters: the delimiters must be added to the pattern when using regular functions compatible with Perl. Any character except letters, numbers, and backslash (\) can be used as the separator.
<? Php
// The following regular expressions are valid:
Echo $ m1 = '/<\/\ w + /';
Echo $ m2 = '| (\ d {3})-\ d | Sm ';
Echo $ m3 = '! ^ (? I) php [34]! ';
Echo $ m4 = '{^ \ s + (\ s + )? $ }';
?>
1.2 Atom: The atom contains common characters, such as letters and numbers. non-printable characters, such as spaces and carriage returns. special characters and metacharacters, such as quotation marks, *, and +, escape with "\". Custom atomic tables, such as [apj] and [a-z]. common character types, such as \ d and \ D.
<? Php
// The following two values are equivalent, matching the email
$ Mail1 = '/^ [0-9a-zA-Z] + @ [0-9a-zA-Z] + (\. [0-9a-zA-Z] +) {0, 3} $ /';
$ Mail2 = '/^ \ w + @ \ w + (\. \ w +) {0, 3} $ /';
?>
1.3 RMB character: a character with special meanings used to construct a regular expression. Perl can use various metacharacters to search and match, such as *, +, and ,?. Common metacharacters are as follows:
1.4 pattern modifier: used outside the regular expression's delimiters to extend the regular expression's functions in terms of matching and replacement.
2. Perl-compatible regular expression functions
2.1 preg_match (string pattern, string subject [, array matches]): used for searching and matching strings. Parameter description:
Pattern is a regular expression, and subject is a string to be processed. optional matches is used to save the matching results for each sub-pattern of pattern. matches [0] stores the overall content that matches pattern, matches [1] stores the matching content in the first parentheses in pattern, and so on.
<? Php
Header ("content-type: text/html; charset = utf8 ");
$ Pattern = '/(http): \ // (www )\. ([^ \. \/] + )\. (com | net | org)/I ';
$ Subject = "my blog: http://www.ido321.com ";
If (preg_match ($ pattern, $ subject, $ matches )){
Echo "the search URL is:". $ matches [0]."
"; // 1st elements in the array save the entire matching result
Echo "the protocol in the URL is:". $ matches [1]."
"; // 2nd elements in the array save 1st word expressions
Echo "the host in the URL is:". $ matches [2]."
"; // 3rd elements in the array Save 2nd word expressions
Echo "the domain name in the URL is:". $ matches [3]."
"; // 4th elements in the array save 3rd word expressions
Echo "the top field in the URL is:". $ matches [4]."
"; // 5th elements in the array save 4th word expressions
}
?>
Result
The preg_match_all () function is similar to the preg_match () function. The difference is that the former will always match to the end of the string, and the latter will stop matching after the first match.
2.2 preg_grep (string pattern, array iput): matches the elements in the array and returns the array units that match the regular expression. Parameter description:
Pattern is a regular expression and input is an array to be matched.
<? Php
$ Arr = array ('Linux RedHat9.0 ', 'apache2. 100', 'mysql5. 100', 'php5. 100', 'lamp', '123 ');
$ Version = preg_grep ('/^ [a-zA-Z] + (\ d | \.) + $/', $ arr );
// Output: Array ([1] => Apache2.2.9 [2] => MySQL5.0.51 [3] => PHP5.2.6)
Print_r ($ version );
?>
2.3 preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit]): string replacement. Note:
This function searches subject for matching items with pattern and replaces them with replacement. Limit is used to limit the number of matches, that is, the number of replicas.
<? Php
$ Pattern = '/<[\/\!] *? [^ <>] *? /Is ';
$ Text = 'this text hasBoldAndUnderlinedAndItalics';
Echo preg_replace ($ pattern, "", $ text); // replace all HTML tags with null
Echo preg_replace ($ pattern, "", $ text, 2); // replace the first two HTML tags with the value
?>
2.4 preg_split (string pattern, string subject [, int limit [, int flags]): splits a string. Note:
The function returns an array. The array element contains the string that matches with pattern in the subject as the boundary. for the meaning of limit, see 2.3. for the meaning of flags, see documentation.
<? Php
// Splits the string by any number of spaces
$ Kerwords = preg_split ("/[\ s,] +/", "hypertext language, programming ");
// Output: Array ([0] => hypertext [1] => language, [2 [=> programming)
Print_r ($ kerwords );
?>
PHP Regular Expression + string extraction
$ Str = '{"a": 1234567890, "B": "u", "birthday": "2000-01-01", "gender": "1", "location ": "123456", "login_ip": "123.123.123.123", "login_time": 1234567890, "id": "1234567", "sign": "0bcbdea54d1f2c3c75b058eb5d2ae124 "}';
$ User_id = "";
If (preg_match_all ('| "id": "(\ S + ?) "| ', $ Str, $ reg ))
{
$ User_id = $ reg [1] [0];
}
Echo "UserID is:". $ user_id;
?>
Output result:
UserID is: 1234567
In PHP, how does one add content to a string using a regular expression?
You can use regular expressions or php directly. it is easier to use php:
$ Str = str_replace ("</Head> ", $ str );
Character 1. features of the string type 1. PHP is a weak language, and other data types can be directly applied to strings...