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 7000015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:37:00+00:00 2026-05-27T20:37:00+00:00

I asked a similar question, but it was closed for being too broad. Basically,

  • 0

I asked a similar question, but it was closed for being too broad. Basically, I have a bunch of questions like this. I’m hoping just asking one will be easier. I’ve tried some different ways to solve this, but none of them actually work.

I have a text file with a lot of data. The only data that I’m interested in falls between two brackets, “(” “)”. I’m wondering how to get each instance of info that lies between brackets into an array.

The code I’m using right now returns ArrayArray:

function get_between($startString, $endString, $myFile){
  preg_match_all('/\$startString([^$endString]+)\}/', $myFile, $matches);
  return $matches;
}
$myFile = file_get_contents('explode.txt');
$list = get_between("&nbsp(", ")", $myFile);
foreach($list as $list){
  echo $list;
}
  • 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-05-27T20:37:01+00:00Added an answer on May 27, 2026 at 8:37 pm
    <?php
    function get_between($startString, $endString, $myFile){
      //Escape start and end strings.
      $startStringSafe = preg_quote($startString, '/');
      $endStringSafe = preg_quote($endString, '/');
      //non-greedy match any character between start and end strings. 
      //s modifier should make it also match newlines.
      preg_match_all("/$startStringSafe(.*?)$endStringSafe/s", $myFile, $matches);
      return $matches;
    }
    $myFile = 'fkdhkvdf(mat(((ch1)vdsf b(match2) dhdughfdgs (match3)';
    $list = get_between("(", ")", $myFile);
    foreach($list[1] as $list){
      echo $list."\n";
    }
    

    I did this and it seems to work. (Obviously, you’ll need to replace my $myFile assignment line with your file_get_contents statement.) A few things:

    A: Variable replacement won’t occur with single-quotes. So your preg_replace_all regular expression won’t work as a result. As it literally adds $startString to your expression instead of (. (I also removed the check for } at the end of the matched string. Add it back in if you need it with \\} just before the ending delimiter.)

    B: $list will be an array of arrays. I believe by default, index zero will contain all full matches. index one will contain the first subpattern match.

    C: This only works so long as $endString will not ever be found inside of a subpattern you are attempting to match. Say, if you expect (matc(fF)) to give you matc(fF), it won’t. It’ll give you match(fF. You’ll need a more powerful parser if you want to get the former result in this case.

    Edit: The get_between function here should work with &nbsp;( and )} as well, or whatever else you’d want.

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

Sidebar

Related Questions

Similar questions have been asked, but nothing exactly like mine, so here goes. We
I have asked a similar question to this but for GMail and I was
Ok, I have previously asked a similar question to this but it was voted
Similar questions have been asked but not quite similar enough! Given this structure: <p
I asked a similar question about this previously, but I did not specify that
I have asked a similar question before, but I didn't have a firm grasp
There have been some similar questions asked regarding Grid views, but none have been
similar questions have been asked before but I cant find an exact match to
I've already asked a similar question but this is slightly different so i Thought
I already asked a similar question but this one is a bit different/specific: I'm

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.