Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:57:32+00:00 2026-06-12T12:57:32+00:00

How would I use str_replace to find every ending square bracket ‘]’ and remove

  • 0

How would I use str_replace to find every ending square bracket ‘]’ and remove 2 or less linebreaks from the end. So, if there were 1 or 2 line breaks after ] it’d remove all. If there were 3 you’d end up with 1 after the ]. 4 you’d end up with 2 after etc.

Thanks for your help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:57:33+00:00Added an answer on June 12, 2026 at 12:57 pm

    Since you mentioned str_replace, I assume that you are using PHP (you should add a tag for that if this is the case!). And since you tagged regular expressions you might want to use preg_replace instead. This should work rather platform-independently then:

    $newstr = preg_replace('/\](?:\r\n|\n|\r){1,2}/', ']', $str);
    

    where $str is obviously your original string.

    Note that the ] gets escaped, because it’s a special regex character (used for character classes). The round brackets just contain a list of all possible linebreaks. The ?: is optional and more relevant for complex regular expressions where you need to access captured subpatterns (in fact the ?: says, don’t match this subpattern, because I don’t need it). And the {1,2} simply says, match one or two of those. And since regular expressions are greedy by default, it will take two if it can.

    EDIT: One more thought. You could solve this with str_replace, but not so well in one go for all possible linebreaks (because str_replace doesn’t support alternatives). This example might give you an idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use regular expression to find every instances of a regular
I have the following string and would like to use str_replace or preg_replace to
I would use a multi dimensional gaussian modell for regression. Rasmussen has a book
I would use Jmagick into an Android Project. Is it possible? I have to
I thought I would use a stored routine to clean up some of my
In .NET I would use System.Diagnostics.Trace... What would I use in C or C++
I just would like to know what format and codec would use the least
What are the main reasons someone would use HTML's tbody in a table? Is
I'm having trouble understanding why I would use a context.xml file to declare a
I am writing a Facebook application that would use a Postgres DB along with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.