PHP uses regular expressions to extract characters in a string with angle brackets, parentheses, brackets, curly braces

Source: Internet
Author: User

 php uses regular expressions to extract the characters descriptor in a string with angle brackets <>, parentheses (), brackets [], curly braces {}, and a friend in need can refer to the following

&nbsp; &nbsp; Code as follows: $str = "Hello &lt; i &gt; (love) [Beijing]{Tiananmen}";&nbsp; &nbsp; echo F1 ($STR); Back to Hello &nbsp; echo f2 ($STR); Return me &nbsp; echo f3 ($STR); Back to Love &nbsp; echo f4 ($STR); Return to Beijing &nbsp; echo f5 ($STR); Back to Tiananmen &nbsp; &nbsp; function F1 ($str) &nbsp; {&nbsp; $result = Array ();&nbsp; Preg_match_all ("/^ (. *) (?:&lt;)/I", $STR, $ Result);&nbsp; return $result [1][0];&nbsp;}&nbsp; &nbsp; function F2 ($str) &nbsp; {&nbsp; $result = Array ();&nbsp; preg_m Atch_all ("/(?:&lt;) (. *) (?:&gt;)/I", $str, $result);&nbsp; return $result [1][0];&nbsp;}&nbsp; &nbsp; function F3 ($STR) &nbsp; {&nbsp; $result = Array ();&nbsp; Preg_match_all ("/(?:() (.*) (?:)) /i ", $str, $result);&nbsp; return $result [1][0];&nbsp;}&nbsp; &nbsp; function f4 ($str) &nbsp; {&nbsp; $result = Array (); &N Bsp Preg_match_all ("/(): [) (. *) (?:])/I", $str, $result);&nbsp; return $result [1][0];&nbsp;}&nbsp; &nbsp; function f5 ($STR) &nbsp; {&nbsp; $result = Array ();&nbsp; Preg_match_all ("/(?: {) (. *) (?:})/I", $str, $result);&nbsp; return $result [1][0]; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; PS: (?: character) indicates that this character is not captured. It seems that PHP does not support converting characters to parentheses. &nbsp; Otherwise you can insert a glance around it and you can loop through it. &nbsp; PS2: Look around: (?!) (?=) (? &lt;!) (? &lt;=) &nbsp; has a less than match on the right side, no match on the left. Exclamation marks are unequal, equals numbers represent equality. &nbsp; PS3: Verified by validator, see Resources for Validator. &nbsp;

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.