且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP/REGEX:在括号内获取一个字符串

更新时间:2022-03-13 16:58:47

除了转义的括号外,您还需要添加捕获括号.

You just need to add capturing parenthesis, in addition to your escaped parenthesis.

<?php
    $in = "hello (world), my name (is andrew) and my number is (845) 235-0184";
    preg_match_all('/\(([A-Za-z0-9 ]+?)\)/', $in, $out);
    print_r($out[1]);
?>

这将输出:

Array ( [0] => world [1] => is andrew [2] => 845 )