Heim  >  Artikel  >  Backend-Entwicklung  >  php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符_PHP教程

php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:34:331173Durchsuche

 PHP使用正则表达式提取字符串中尖括号、小括号()、中括号[]、大括号{}中的字符示例,需要的朋友可以参考下

 

 代码如下:

$str="你好(爱)[北京]{天安门}"; 

 

echo f1($str); //返回你好 

echo f2($str); //返回我 

echo f3($str); //返回爱 

echo f4($str); //返回北京 

echo f5($str); //返回天安门 

 

function f1($str) 

$result = array(); 

preg_match_all("/^(.*)(?:

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: (?:字符) 表示不捕获这个字符。貌似PHP不支持将字符换成括号。 

否则的话可以将环视给嵌套进去,就可以循环匹配了。 

PS2:环视:(?!) (?=) (?

有小于号的在右侧匹配,没有的在左侧匹配。感叹号表示不等,等于号表示相等。 

PS3:都过了验证器的验证,验证器见参考资料。 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/750625.htmlTechArticlePHP使用正则表达式提取字符串中尖括号、小括号()、中括号[]、大括号{}中的字符示例,需要的朋友可以参考下 代码如下: $str=你好我(爱)[北京...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn