string and regular expressions in PHP, PHP regular expression _php tutorial

Source: Internet
Author: User
Tags php regular expression

strings and regular Expressions in PHP, PHP regular expressions


I. Characteristics of String types

1, PHP is a weakly typed language, other data types can be directly applied to string function operation.

1:
Output 345
Output 345
Look for Hello constants first, and if not found, use Hello as a string
    2, the string can be used as an "array", is a collection of characters.

1:
   3:echo $str [0];
   5:echo $str [2];
          However, the string is not a true array, and the function of the array cannot be used. such as COUNT ($STR) does not return a string length. The PHP engine cannot differentiate between characters and arrays, resulting in two semantics. Since PHP4, curly braces have been used instead of square brackets.

1:
   3:"www.ido321.com";
   5:echo $str {1};
   7:  ?>

3. Double quotation mark variable parsing

In PHP, when you define a string with double quotes or delimiters, the variables are parsed.

1:
"Dwqs",' Add '"www.ido321.com");
Can parse, but cannot use quotation marks in square brackets
   5:echo"{$arr [' name ']}";  //Can parse, with curly braces containing elements, name without quotation marks is also possible
   7://Assume that there is an object $square
Can parse
Can not parse, with curly braces to solve
Can parse

Second, the string output function

Iii. commonly used string format functions

Most of the string handling functions of ps:php do not modify the source string, but instead return the new string

Four, the regular expression

The regular expression describes a pattern of string matching, which is composed of three parts: Atomic, meta-character, and pattern modifier, through which the string is matched, searched, replaced, and delimited in a particular function.

In PHP, there are two regular library of processing functions: 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.

The Perl language-compatible regular expression handler:

1. Grammar

1.1 delimiter: When using patterns in a Perl-compatible regular function, you must add a delimiter to the pattern. Any character other than letters, numbers, and backslashes (\) can be used as a bounding symbol

1:
   3:echo'/<\/\w+/';
   5:echo'!^ (? i) php[34]! ';
   7:  ?>

1.2 Atoms: atoms contain ordinary characters such as letters, numbers, nonprinting characters , such as spaces, carriage returns, and special characters and metacharacters characters , such as quotation marks, *, +, etc., which must be escaped with "\"; Custom Atomic Tables , such as [APJ], [A-z], Universal character types , such as \d, \d.

1:
   3:'/^[0-9a-za-z]+@[0-9a-za-z]+ (\.[ 0-9a-za-z]+) {0,3}$/';
   5:  ?>

1.3 Metacharacters: A character that is used to construct a regular expression with a special meaning. Perl can use a variety of meta-characters to search for matches, such as *, +,? Common meta characters are as follows

1.4 mode modifier: Used outside the regular delimiter, extending the function of the regular in matching, replacing, and so on.

2. perl-compatible regular expression functions

2.1 Preg_match (String pattern,string Subject[,array matches]): Used to find and match strings. Parameter description:

Pattern is a regular, subject is a string that needs to be processed, an optional matches is used to match the results of each sub-pattern stored in pattern, matches[0] preserves the overall content that matches the pattern, matches[1] Preserves the matching content in the first parenthesis in pattern, and so on.

1:
);
   4:"my blog: http://www.ido321.com";
   6:     echo"Search URL is:". $matches [0]."
"; //The 1th element of the array holds the entire matching result
; The 2nd element of the array holds the 1th-word expression
; The 3rd element of the array holds the 2nd-word expression
; The 4th element of the array holds the 3rd-word expression
; The 5th element of the array holds the 4th-word expression
  :  ?>

Results

Preg_match_all () is similar to the Preg_match () function, where the former is matched to the end of the string, which stops matching after the first match.

2.2 Preg_grep (String Pattern,array iput): Matches an element in an array, returning an array cell that matches a regular match. Parameter description:

pattern is regular, and input is an array that needs to be matched.

1:
   3:  $version = preg_grep ('/^[a-za-z]+ (\d|\.) +$/', $arr);
   5://output: Array ([1]=>apache2.2.9 [2]=>mysql5.0.51 [3]=>php5.2.6]
   7:  ?>

2.3 preg_replace (mixed pattern,mixed replacement,mixed subject[,int limit]): string substitution. Description

The function searches the subject for a match with pattern and replaces it with replacement. Limit is used to restrict the number of matches, that is, the number of replacements.

1:
   3:' This text is bold and underlined and Italic ';
Replace all HTML tags with an empty
Value replaces first 2 HTML tags
              2.4  preg_split (string pattern,string subject[,int Limit[,int flags]): Splits the string. Description

The function returns an array. The array element contains a string that is split in subject with the pattern match as the boundary, and the limit means see 2.3,flags meaning refer to the documentation.

   2://split string by any number of spaces
   4:  
   6:  print_r ($kerwords);
Source: http://www.ido321.com/612.html


Extracting strings in PHP--regular

$text = '

</p>

。。。。。。 </p>

test</p> ';

Preg_match_all ('/print_r ($m);

The regular expression of the PHP intercept string, the urgent

$s = "soproxy.appspot.com/...opular";
Preg_match ('/v= (. *?) &/', $s, $matched);
Echo ($matched [1]);

http://www.bkjia.com/PHPjc/878810.html www.bkjia.com true http://www.bkjia.com/PHPjc/878810.html techarticle PHP string and regular expressions, PHP regular expression one, the character string type 1, PHP is a weak type language, other data types can be directly applied to the string function ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.