I want to remove everything inside braces. For example, if string is:
[hi] helloz [hello] (hi) {jhihi}
then, I want the output to be only helloz
.
I am using the following code, however it seems to me that there should be a better way of doing it, is there?
$name = "[hi] helloz [hello] (hi) {jhihi}";
$new = preg_replace("/\([^)]+\)/","",$name);
$new = preg_replace('/\[.*\]/', '', $new);
$new = preg_replace('/\{.*\}/', '', $new);
echo $new;
I made a function for that. It replaces the text in braces (including them) with the text you want. It also works with nested braces.
foo(foo(foo))
withbar
as replace will returnfoobar
. It also allows collection of replaced text in a var (In such situation, previous example will showfoo(foo)
as replaced text) with your own key. It does not use regex and allows not only braces, but everything else (but only if its single character... sorry).Arguments:
$searchStart
is the starting character of search.$searchEnd
is the ending character of search$replace
is the replacement text.$subject
is the text where all replaces happenOptional elements:
&$assignValue
is reference to the array variable you want to store replaced text with. Optional$addValue
is starting number in counting replacement. It isn't a reference, but if not equal to false (0 can still be), it will be put after the key in$assignValue
. It is before$valueKey
, because when$valueKey
is left empty, it will count like in a normal array (if its not false, of course - in example - 0), likearray(0 => "foo", 1 => "bar")
. Increases by 1 every replacement.$inReplace
(if true) will also put$addValue
after replacement, increasing by 1 every action.$valueKey
is the key used for all items in the replaced text array. It is highly recommended to set$addValue
not false when using - it will return only last item replaced!Return value:
It does not change any of arguments, but
$assignValue
. It returns modified$subject
and set$assignValue
to array of replaced texts.Example:
return:
test: ?_MATH0, test: ?_MATH1
$vars
=Hope it worked. For me it did. If you misunderstood something or found errors in reference, just comment below. (For errors - edit if you know and tested your solution)
This should work:
Paste it somewhere like: http://writecodeonline.com/php/ to see it work.
[old answer]
If needed, the pattern that can deal with nested parenthesis and square or curly brackets:
[EDIT]
A pattern that only removes well balanced parts and that takes in account the three kinds of brackets:
This pattern works well but the additional type check is a bit overkill when the goal is only to remove bracket parts in a string. However it can be used as a subpattern to check if all kind of brackets are balanced in a string.
A pattern that removes only well balanced parts, but this time, only the outermost type of bracket is taken in account, other types of brackets inside are ignored (same behavior than the old answer but more efficient and without the useless conditional tests):