I need to remove the brackets "[" and "]" from $widget_text
in the variable value below and store the result in $widget_id
.
我需要从下面变量值的$ widget_text中删除括号“[”和“]”,并将结果存储在$ widget_id中。
$widget_text = '[widget_and-some-text]';
$widget_id = ?;
Use preg_replace
, str_replace
or something else?
使用preg_replace,str_replace还是别的什么?
3 个解决方案
#1
45
There are several methods available, and they can sometimes be made to perform exactly the same task, like preg_replace/str_replace. But, perhaps you want to remove brackets only from the beginning or end of the string; in which case preg_replace works. But, if there could be several brackets, preg_replace can do the job too. But trim is easier and makes more sense.
有几种方法可用,有时可以使它们执行完全相同的任务,例如preg_replace / str_replace。但是,也许你只想从字符串的开头或结尾删除括号;在这种情况下preg_replace工作。但是,如果可以有多个括号,preg_replace也可以完成这项工作。但修剪更容易,更有意义。
preg_replace() - removes beginning and trailing brackets
preg_replace() - 删除开始和结尾括号
$widget_id = preg_replace(array('/^\[/','/\]$/'), '',$widget_text);
str_replace() - this removes brackets anywhere in the text
str_replace() - 这将删除文本中任何位置的括号
$widget_id = str_replace(array('[',']'), '',$widget_text);
trim() - trims brackets from beginning and end
trim() - 从开头和结尾修剪括号
$widget_id = trim($widget_text,'[]')
substr() - does the same as trim() (assuming the widget text does not include any closing brackets within the text)
substr() - 与trim()相同(假设窗口小部件文本不包含文本中的任何右括号)
$widget_id = substr($widget_text,
$start = strspn($widget_text, '['),
strcspn($widget_text, ']') - $start
);
#2
6
$widget_id = str_replace('[', '', str_replace(']', '', $widget_text));
#3
1
If the brackets are always at first and last position, use this:
如果括号始终位于第一个和最后一个位置,请使用:
$widget_id = substr($widget_text, 1, strlen($widget_text)-2);
I think this is a faster way...
我认为这是一种更快的方式......
#1
45
There are several methods available, and they can sometimes be made to perform exactly the same task, like preg_replace/str_replace. But, perhaps you want to remove brackets only from the beginning or end of the string; in which case preg_replace works. But, if there could be several brackets, preg_replace can do the job too. But trim is easier and makes more sense.
有几种方法可用,有时可以使它们执行完全相同的任务,例如preg_replace / str_replace。但是,也许你只想从字符串的开头或结尾删除括号;在这种情况下preg_replace工作。但是,如果可以有多个括号,preg_replace也可以完成这项工作。但修剪更容易,更有意义。
preg_replace() - removes beginning and trailing brackets
preg_replace() - 删除开始和结尾括号
$widget_id = preg_replace(array('/^\[/','/\]$/'), '',$widget_text);
str_replace() - this removes brackets anywhere in the text
str_replace() - 这将删除文本中任何位置的括号
$widget_id = str_replace(array('[',']'), '',$widget_text);
trim() - trims brackets from beginning and end
trim() - 从开头和结尾修剪括号
$widget_id = trim($widget_text,'[]')
substr() - does the same as trim() (assuming the widget text does not include any closing brackets within the text)
substr() - 与trim()相同(假设窗口小部件文本不包含文本中的任何右括号)
$widget_id = substr($widget_text,
$start = strspn($widget_text, '['),
strcspn($widget_text, ']') - $start
);
#2
6
$widget_id = str_replace('[', '', str_replace(']', '', $widget_text));
#3
1
If the brackets are always at first and last position, use this:
如果括号始终位于第一个和最后一个位置,请使用:
$widget_id = substr($widget_text, 1, strlen($widget_text)-2);
I think this is a faster way...
我认为这是一种更快的方式......