Home > Backend Development > PHP Tutorial > PHP uses regular expressions to extract characters in angle brackets, parentheses, square brackets, and braces in a string_PHP Tutorial

PHP uses regular expressions to extract characters in angle brackets, parentheses, square brackets, and braces in a string_PHP Tutorial

WBOY
Release: 2016-07-13 10:34:33
Original
1230 people have browsed it

PHP uses regular expressions to extract character examples in angle brackets <>, parentheses (), square brackets [], and curly brackets {} in a string. Friends who need it can refer to the following

The code is as follows:

$str="Hello(love)[Beijing]{Tiananmen}";

echo f1($str); //return hello

echo f2($str); //return to me

echo f3($str); //return 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) means not to capture this character. It seems that PHP does not support changing characters into brackets.

Otherwise, you can nest the lookaround and loop the matching.

PS2: Look around: (?!) (?=) (?

If there is a less than sign, it will be matched on the right side, and if there is not, it will be matched on the left side. The exclamation mark indicates inequality, and the equal sign indicates equality.

PS3: All have been verified by the authenticator. For the authenticator, see the reference materials.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/750625.htmlTechArticlePHP uses regular expressions to extract angle brackets, parentheses (), brackets [], and braces in a string Examples of characters in {}, friends in need can refer to the following code: $str=Hello, I (love) [Beijing...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template