I have an array with some info. For example:
我有一个包含一些信息的数组。例如:
(writer) &
or
with (additional dialogue)
(附加对话)
I want to clean this so I only get the text between the parenthesis () and clear everything else
我想清理这个,所以我只得到括号()之间的文本,并清除其他所有内容
result:
writer
or
additional dialogue
5 个解决方案
#1
$string = "this (is (a) test) with (two parenthesis) duh";
For a string like this you can use preg_match_all and use implode.
对于这样的字符串,您可以使用preg_match_all并使用implode。
$string = "this (is (a) test) with (two parenthesis) duh";
$regex = '#\((([^()]+|(?R))*)\)#';
if (preg_match_all($regex, $string ,$matches)) {
echo implode(' ', $matches[1]);
} else {
//no parenthesis
echo $string;
}
Or you can use preg_replace, but with multiple parenthesis you'll lose the whitespace between them.
或者您可以使用preg_replace,但是如果使用多个括号,您将丢失它们之间的空格。
$regex = '#[^()]*\((([^()]+|(?R))*)\)[^()]*#';
$replacement = '\1';
echo preg_replace($regex, $replacement, $string);
I got a lot of help from this page, Finer points of PHP regular expressions.
我从这个页面得到了很多帮助,PHP正则表达式更精细。
#2
The easiest way will be with a regular expression:
最简单的方法是使用正则表达式:
preg_match_all('/\((.*?)\)/', $input, $matches);
$matches[1]
, $matches[2]
, etc will contain everything that was between parentheses in $input. That is, $matches[1]
will have whatever was between the first set of parentheses, and so on (to handle cases with multiple sets).
$ matches [1],$ matches [2]等将包含$ input中括号之间的所有内容。也就是说,$ matches [1]将具有第一组括号之间的任何内容,依此类推(以处理具有多个集合的情况)。
#3
$matches = array();
$num_matched = preg_match_all('/\((.*)\)/U', $input, $matches);
#4
function getInbetweenStrings($start, $end, $str){
$matches = array();
$regex = "/$start([a-zA-Z0-9_]*)$end/";
preg_match_all($regex, $str, $matches);
return $matches[1];
}
for examle you want the array of strings(keys) between {} in following example, where '/' doesn't fall in-between
例如,在下面的示例中,您需要{}之间的字符串(键)数组,其中'/'不介于其间
$str = "C://{ad_custom_attr1}/{upn}/{samaccountname}";
$str_arr = getInbetweenStrings('{', '}', $str);
print_r($str_arr);
#5
Using above in a replace
在上面使用替换
echo preg_replace('/\(([\w]{1,2})\)/',"(s\\1)",'(Gs) Main Hall');
results in
(sGs) Main Hall
#1
$string = "this (is (a) test) with (two parenthesis) duh";
For a string like this you can use preg_match_all and use implode.
对于这样的字符串,您可以使用preg_match_all并使用implode。
$string = "this (is (a) test) with (two parenthesis) duh";
$regex = '#\((([^()]+|(?R))*)\)#';
if (preg_match_all($regex, $string ,$matches)) {
echo implode(' ', $matches[1]);
} else {
//no parenthesis
echo $string;
}
Or you can use preg_replace, but with multiple parenthesis you'll lose the whitespace between them.
或者您可以使用preg_replace,但是如果使用多个括号,您将丢失它们之间的空格。
$regex = '#[^()]*\((([^()]+|(?R))*)\)[^()]*#';
$replacement = '\1';
echo preg_replace($regex, $replacement, $string);
I got a lot of help from this page, Finer points of PHP regular expressions.
我从这个页面得到了很多帮助,PHP正则表达式更精细。
#2
The easiest way will be with a regular expression:
最简单的方法是使用正则表达式:
preg_match_all('/\((.*?)\)/', $input, $matches);
$matches[1]
, $matches[2]
, etc will contain everything that was between parentheses in $input. That is, $matches[1]
will have whatever was between the first set of parentheses, and so on (to handle cases with multiple sets).
$ matches [1],$ matches [2]等将包含$ input中括号之间的所有内容。也就是说,$ matches [1]将具有第一组括号之间的任何内容,依此类推(以处理具有多个集合的情况)。
#3
$matches = array();
$num_matched = preg_match_all('/\((.*)\)/U', $input, $matches);
#4
function getInbetweenStrings($start, $end, $str){
$matches = array();
$regex = "/$start([a-zA-Z0-9_]*)$end/";
preg_match_all($regex, $str, $matches);
return $matches[1];
}
for examle you want the array of strings(keys) between {} in following example, where '/' doesn't fall in-between
例如,在下面的示例中,您需要{}之间的字符串(键)数组,其中'/'不介于其间
$str = "C://{ad_custom_attr1}/{upn}/{samaccountname}";
$str_arr = getInbetweenStrings('{', '}', $str);
print_r($str_arr);
#5
Using above in a replace
在上面使用替换
echo preg_replace('/\(([\w]{1,2})\)/',"(s\\1)",'(Gs) Main Hall');
results in
(sGs) Main Hall