Php string and regular expression, php Regular Expression

Source: Internet
Author: User
Tags php regular expression printable characters

Php string and regular expression, php Regular Expression

I. Features of the string type

1. PHP is a weak type language. Other data types can be directly applied to string function operations.

1: <? Php
// Output 345
// Output 345
// Search for the hello constant first. If it is not found, use "hello" as a string.
2. A string can be used as an "array" and is a collection of characters.

 

1: <? Php
   3: echo $str[0];
   5: 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.

 

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

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.

1: <? Php
"dwqs",'add' => "www.ido321.com");
// It can be parsed, but quotation marks cannot be used in square brackets.
5: echo "{$ arr ['name']}"; // It can be parsed. elements are included in curly brackets. It is also possible to name without quotation marks.
7: // assume that the object $ square exists.
; // Can be parsed
; // It cannot be parsed and should be resolved in curly brackets
; // 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.

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

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.

1: <? Php
   3: $mail1 = '/^[0-9a-zA-Z]+@[0-9a-zA-Z]+(\.[0-9a-zA-Z]+){0,3}$/';
   5: ?>

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.

1: <? Php
);
4: $ subject = "my blog: http://www.ido321.com ";
6: echo "the search URL is:". $ matches [0]. "<br/>"; // The array contains 1st elements to save the entire matching result.
; // 2nd elements in the array save 1st word expressions
; // 3rd elements in the array save 2nd word expressions
; // 4th elements in the array save 3rd word expressions
; // 5th elements in the array save 4th word expressions
  12: ?>

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.

1: <? Php
   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 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.

1: <? Php
3: $ text = 'The text contains <B> bold </B>, <u> underlined </u>, and <I> italic </I> ';
// Replace all HTML tags with null
// 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.

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

 


Extract string-regular in PHP

<? Php
$ Text = '<p> </p>
<P> ...... </P>
<P> test </p> ';

Preg_match_all ('/Print_r ($ m );

Php intercepts the regular expression of the string, urgent

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

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.