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

  • SEARCH
  • Home
  • 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 1091517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:32:28+00:00 2026-05-16T23:32:28+00:00

$variable = ‘of course it is unnecessary [http://google.com], but it is simple[very simple], and

  • 0
$variable = 'of course it is unnecessary [http://google.com], 
but it is simple["very simple"], and this simple question clearly
needs a simple, understandable answer [(where is it?)] in plain English'

Value of this variable everytime changes.

What I trying to do is to get the text from [...]. So, if there is [(google)], the match should be (google).

I’m searching for a solution, which can do each of these actions:

  1. get all matches of […], write into $all
  2. get only the first match, write into $first
  3. get only the last match, write into $last
  4. remove all matches of […] from the variable (erase)
  5. remove only first match
  6. remove only last match

Tried different regex for this, like /[\(.*?\)]/, but the results aren’t what one might expect.

  • 1 1 Answer
  • 3 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-05-16T23:32:30+00:00Added an answer on May 16, 2026 at 11:32 pm

    This should do it:

    $variable = 'of course it is unnecessary [http://google.com], 
    but it is simple["very simple"], and this simple question clearly
    needs a simple, understandable answer [(where is it?)] in plain English';
    
    preg_match_all("/(\[(.*?)\])/", $variable, $matches);
    
    $first = reset($matches[2]);
    $last = end($matches[2]);
    $all = $matches[2];
    
    # To remove all matches
    foreach($matches[1] as $key => $value) {
        $variable = str_replace($value, '', $variable);
    }
    
    # To remove first match
    $variable = str_replace($first, '', $variable);
    
    # To remove last match
    $variable = str_replace($last, '', $variable);
    

    Note that if you use str_replace to replace the tags, all similar occurences of the tags will be removed if such exist, not just the first.

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

Sidebar

Related Questions

The variable @photo contains this XML: <photo id=2733> <urls> <url type=photopage>http://www.flickr.com/photos/bees/2733/</url> </urls> </photo> I
Consider variable like var url='http://www.example.com/index.php?id=ss'; or var url='http://www.example.com/dir1/dir2/index.php'; In these variables, i want to
Variable is used in 1 task only, but the question is: can it be
The variable asynchExecutions does get changed, but it doesn't change the reference variable. Simple
supposed there is a variable named: src=http://www.test.com/1455/||src=http://www.test.com/image/1456/|| now i want to get the url
Variable names in haskell need to be in small case, but How to declare
I have the following variable: pageID = 7 I'd like to increment this number
This variable is defined in xyz.cpp const int i = 10; The below main()
$image variable gives this code (when echo is used): <img src=/image.png width=100 height=147 alt=
The variable strCSSClass often has a value but sometimes is empty. I do not

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.