Jump to content

[SOLVED] remove [square brackets and contents] from string


andyd34

Recommended Posts

Ho wwould I remove square brackets and there contents from a string, have tried

 

$option_title = preg_replace('`\[\w+\s+\w+\s+#\d+\]`', '', $option_title);

 

and

 

$option_title = preg_replace('/\[(^])*\)|]([])]/', '', $option_title);

 

and

 

$option_title = preg_replace('/\([^)]*\)|[()]/', '', $option_title);

 

any suggestions

Link to comment
Share on other sites

Granted, these solutions will not take on nested square brackets (in the event the actual source string in question involves this). One possible solution could be:

 

$str = 'This is a test to remove nested [[s]q[u]a[r]e] brackets [[from [t[hi]s] source [string]!]]';
echo $str = preg_replace('#\[[^[\]]*(??R)|.*?)+\]#', '', $str);

 

EDIT - If the chunk of square brackets extends multiple lines, simply add the s modifier.

 

@jenk, g is not a known modifier.

Link to comment
Share on other sites

I believe that the starter isn't interested in this thread anymore. I have tested my code and it works for me. Therefore, I request a mod to place a [solved] in the topic name.

 

Thank you

 

PS: Sorry for double posting. Just bringing it into the views of the mods :)

Link to comment
Share on other sites

Sorry, if u thought I meant it that way. I meant to say that I have tested my solution and it does work. So it is solved for sure.

 

Many of the solutions above may work, but I have not tested them so i cannot say for sure.

 

Actually, upon testing

#\[.*\]#

, what Crayon Violent and MrAdam have said, I would say it is not recommended. I say this because it will also remove what comes before the brackets.

 

ex. fri[hello]day will become "day".

 

If that does not affect you, then feel free to use it.

 

Upon using

(\\[.*?\\])

, you will get "friday" :)

 

Just throwing it out there :)

Link to comment
Share on other sites

Or even..

 

#\[.*\]#

 

Why don't you try running that through this string and see what happens:

 

[blahblah] some random stuff [more blah]

 

Sorry, i didn't see that post. I only looked at what Crayon Violent had written.

 

So I stand corrected.

#\[.*?\]#

will work :)

Thanks for clearing it up MrAdam :)

Link to comment
Share on other sites

Actually, upon testing

#\[.*\]#

, what Crayon Violent and MrAdam have said, I would say it is not recommended. I say this because it will also remove what comes before the brackets.

 

ex. fri[hello]day will become "day".

 

Really?

 

$str = 'fri[hello]day';
echo $str = preg_replace('#\[.*\]#', null, $str);

 

Aside from MrAdam correcting that post with the use of a lazy quantifier, the issue with using .* (or even .+) is demonstrated in the following: (post #14 shows the potential pitfall of this 'typically reckless' behavior - sometimes this behavior is actually beneifical.. but as a rule?  :anim_reading:).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.