Character of a String type
1, PHP is a weak type language, other data types can generally be directly applied to string function operations.
<?php
echo substr ("123456", 2,4); Output 345
echo substr (123456,2,4); Output 345
echo Hello; First look for the Hello constant, if not found, hello as a string to use
?>
2, the string can be "array", is a collection of characters.
<?php
$str = "Www.jb51.net";
echo $str [0];
echo $str [1];
Echo $str [2];
?>
However, the string is not a true array and cannot use the array's functions. such as COUNT ($STR) does not return the string length. The PHP engine cannot differentiate between characters and arrays, resulting in two of semantics. Since PHP4, brackets have been replaced with curly braces.
<?php
To ensure backward compatibility, the brackets can still be used
$str = www.jb51.net;
echo $str {0};
echo $str {1};
echo $str {2};
?>
3, double quote variable resolution
In PHP, when you define a string with double quotes or delimiters, the variables are parsed.
<?php
$arr = Array (' name ' => ' Dwqs ', ' Add ' => ' www.ido321.com ');
echo "$arr [name]"; Can parse, but cannot use quotation marks in square brackets
echo "$arr [' name ']"; Error
Echo ' {$arr [' name ']} '; Can parse, with curly braces to contain elements, name is not quoted is also possible
Suppose there is an object $square
echo "$square->width"; Can parse
echo "$square->width00 cent"; Can not parse, with curly braces to solve
echo "{$square->width}width00 cent"; Can parse
?>
Second, string output function
Common string format functions
Most of the ps:php string processing functions do not modify the source string, but instead return the new string
Four, regular expressions
A regular expression describes a pattern of string matching, in which the string is matched, found, replaced, and delimited in a particular function by a text pattern consisting of atoms, metacharacters, and pattern modifiers.
In PHP, there are two sets of regular processing libraries: Pcre and POSIX. The former is named after the Preg_ prefix and is compatible with Perl; the latter is named after the Ereg_ prefix. The two functions are similar, but the efficiency of pcre is slightly higher.
Regular expression handler functions that are compatible with Perl language:
1. Grammar
1.1 delimiter: When using patterns in Perl-compatible regular functions, the pattern must be prefixed with a delimiter. Any character other than letters, numbers, and backslashes (\) can be used as a bounding symbol
<?php
The following are legal
echo $m 1 = '/<\/\w+/';
echo $m 2 = ' | (\d{3})-\d| Sm ';
echo $m 3 = '!^ (? i) php[34]! ';
echo $m 4 = ' {^\s+ (\s+)? $} ';
?>
1.2 Atoms: The atom contains ordinary characters, such as letters, numbers, nonprinting characters, such as spaces, carriage returns, special character and metacharacters characters, such as quotes, *, +, etc., must be escaped with "\", custom atomic tables, such as [APJ], [A-z], universal character types, such as \d, \d.
<?php
The following are equivalent, matching e-mail
$mail 1 = '/^[0-9a-za-z]+@[0-9a-za-z]+ (\.[ 0-9a-za-z]+) {0,3}$/';
$mail 2 = '/^\w+@\w+ (\.\w+) {0,3}$/';
?>
1.3-dollar character: a character that is used to construct a regular expression with special meaning. Perl can use a variety of metacharacters to search for matches, such as *, +,? The following meta characters are common
1.4 mode modifier: Used outside the regular delimiter, extending the function of matching, replacing, and so on.
2. perl-compatible regular expression functions
2.1 Preg_match (String pattern,string Subject[,array matches]): for finding and matching strings. Parameter description:
pattern is regular, subject is a string to be processed, and optional matches is used to match the results of each of the patterns, matches[0] preserves the overall content that matches the pattern, matches[1] Saves the matching content in the first parenthesis 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)) {
The URL of the echo search is: ". $matches [0]." <br/> "; The 1th element of the array holds the entire matching result
The protocol in the Echo URL is: ". $matches [1]." <br/> ";//2nd element of the array save the 1th word expression
The host in the Echo URL is: ". $matches [2]." <br/> ";//3rd element of the array save the 2nd word expression
The domain name in the echo URL is: ". $matches [3]." <br/> ";//4th element of the array save the 3rd word expression
The top field in the Echo URL is: ". $matches [4]." <br/> ";//5th element of the array save the 4th word expression
}
?>
Results
The Preg_match_all () is similar to the Preg_match () function, but the former will always match to the end of the string, which stops matching the first time it matches.
2.2 Preg_grep (String Pattern,array iput): matches the elements in the array, returning the array cell that matches the regular match. Parameter description:
pattern is regular and input is an array that needs to be matched.
<?php
$arr = Array (' Linux RedHat9.0 ', ' Apache2.2.9 ', ' MySQL5.0.51 ', ' PHP5.2.6 ', ' LAMP ', ' 100 ');
$version = Preg_grep ('/^[a-za-z]+ (\d|\.) +$/', $arr);
Output: Array ([1]=>apache2.2.9 [2]=>mysql5.0.51 [3]=>php5.2.6)
?>
2.3 preg_replace (mixed pattern,mixed replacement,mixed subject[,int limit]): string substitution. Description
The function searches the subject for matches with pattern and replaces it with replacement. Limit is used to limit the number of matches, that is, the number of substitutions.
<?php
$pattern = '/<[\/\!] *? [^<>]*?/is ';
$text = ' This text has <b> bold </b> and <u> with underscore </u> and <i> Italic </i> ';
Echo preg_replace ($pattern, "", $text); Replace all HTML tags with null
Echo preg_replace ($pattern, "", $text, 2); Value replaces the first 2 HTML tags
?>
2.4 preg_split (String pattern,string subject[,int Limit[,int flags]): Splits the string. Description
function to return an array. The array element contains the strings that are subject by the pattern matching as the boundary, limit meaning see 2.3,flags meaning refer to the documentation.
<?php
//per-number space split string
$kerwords = Preg_split ("/[\s,]+/", "hypertext Language,programming");
Output: Array ([0]=>hypertext [1]=>language,[2[=>programming)
Print_r ($kerwords);
? "