PHP uses regular expressions to extract the characters in the string in angle brackets, parentheses, brackets, curly braces _php Tutorial

Source: Internet
Author: User
PHP uses regular expressions to extract the characters in the string with angle brackets <>, parentheses (), brackets [], curly braces {} Descriptor examples, the friends you need can refer to the following

The code is as follows:

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

Echo F1 ($STR); Back to Hello

echo F2 ($STR); Return to my

echo f3 ($STR); Return to Love

echo f4 ($STR); Back to Beijing

echo f5 ($STR); Back 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) means that this character is not captured. It seems PHP does not support converting characters to parentheses.

Otherwise you can put a look into the nest, you can loop matching.

PS2: Surround: (?!) (?=) (?

There is a less than sign on the right side of the match, no match on the left. An exclamation point is unequal, and an equal sign represents equality.

PS3: Verified by authenticator, see Resources for Validator.

http://www.bkjia.com/PHPjc/750625.html www.bkjia.com true http://www.bkjia.com/PHPjc/750625.html techarticle PHP uses regular expressions to extract strings of angle brackets, parentheses (), brackets [], curly braces {} In the Word descriptor example, the need for friends can refer to the following code: $STR = Hello i (Love) [Beijing ...]

  • 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.