如何提取特殊字符之间的字符串?



我想提取里面的任何文本((。 所需的输出应为"名称"、"土耳其"。 我试过这个

$input ="这是(名称(。我住在(土耳其(。等等,其他括号也是如此"。 $unit = extract_unit($input, '(', '('(; 回声$unit; 函数 extract_unit($string, $start, $end( { $pos = 条带($string, $start(; $str = substr($string, $pos(; $str_two = substr($str, strlen($start((; $second_pos = stripos($str_two, $end(; $str_three = substr($str_two, 0, $second_pos(; $unit = trim($str_three(;删除空格 返回$unit; }

但是,如果文本长度增加,则仅获得第一个输出,即"名称",但需要"土耳其"等值。

尝试使用这个:

$string = 'This is (Name). I live in (Turkey). and so on with other brackets as well';
preg_match_all('/((.*?))/i', $string, $matches);
print_r($matches[1]);

最新更新