[英]How to extract string between special characters?
我想提取()中的任何文本。 所需的输出应为“名称”,“土耳其”。 我已经试过了
$input = "This is (Name). I live in (Turkey). and so on with other brackets as well". $unit = extract_unit($input, '(', ')'); echo $unit; function extract_unit($string, $start, $end) { $pos = stripos($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); // remove whitespaces return $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]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.