PHP uses regular expressions to extract string _php instances of angle brackets, parentheses, brackets, curly braces in a string

Source: Internet
Author: User

Copy Code code as follows:

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

Echo F1 ($STR);//return Hello
echo f2 ($STR);//Back to Me
Echo F3 ($STR);//Back to Love
Echo f4 ($STR);//Back to Beijing
Ech o 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 converting characters to parentheses.
Otherwise, you can nest around, and you can loop through it.
PS2: Look around: (?!) (?=) (? <!) (? <=)
There is a match on the right of the less than number, no match on the left. Exclamation marks are unequal, equals numbers represent equality.
PS3: Verified by validator, see Resources for Validator.

PS: Here again for you to provide 2 very convenient regular expression tools for your reference to use:

JavaScript Regular expression online test tool:
Http://tools.jb51.net/regex/javascript

Regular expression online generation tool:
Http://tools.jb51.net/regex/create_reg

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.