Php uses regular expressions to extract characters in string brackets, parentheses, brackets, and braces _ PHP Tutorial

Source: Internet
Author: User
Php uses regular expressions to extract characters in the brackets, parentheses, brackets, and braces of a string. PHP uses regular expressions to extract character examples in string brackets, parentheses (), brackets [], and braces {}. For more information, see the following code: $ str Hello Me (Love) [Beijing PHP uses regular expressions to extract character examples in string angle brackets <>, parentheses (), brackets [], and braces {}. For more information, see

The code is as follows:

$ Str = "Hello <我> (Love) [Beijing] {Tiananmen Square }";

Echo f1 ($ str); // return Hello

Echo f2 ($ str); // return me

Echo f3 ($ str); // returns love

Echo f4 ($ str); // return to Beijing

Echo f5 ($ str); // Return to Tiananmen

Function f1 ($ str)

{

$ Result = array ();

Preg_match_all ("/^ (.*)(? : <)/I ", $ str, $ result );

Return $ result [1] [0];

}

Function f2 ($ str)

{

$ Result = array ();

Preg_match_all ("/(? : <)(.*)(? :>)/I ", $ str, $ result );

Return $ result [1] [0];

}

Function f3 ($ str)

{

$ Result = array ();

Preg_match_all ("/(? :\()(.*)(? : \)/I ", $ str, $ result );

Return $ result [1] [0];

}

Function f4 ($ str)

{

$ Result = array ();

Preg_match_all ("/(? :\[)(.*)(? : \])/I ", $ str, $ result );

Return $ result [1] [0];

}

Function f5 ($ str)

{

$ Result = array ();

Preg_match_all ("/(? :\{)(.*)(? :\})/I ", $ str, $ result );

Return $ result [1] [0];

}

PS :(? : Character) indicates that this character is not captured. It seems that PHP does not support replacing characters with parentheses.

Otherwise, you can view the loop as nested, and then perform loop matching.

PS2: Huanshi :(?!) (? = )(?

If there is a smaller number, match on the right, and if not, match on the left. Exclamation points indicate unequal, and equal signs indicate equal.

PS3: all are verified by the validators. For more information about the validators, see references.

Examples of characters in parentheses (), brackets [], and braces {}. For more information, see the following code: $ str = Hello, Me (Love) [Beijing...

Related Article

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.