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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:09:20+00:00 2026-06-13T00:09:20+00:00

I am trying to parse text for special include statements to pull in specific

  • 0

I am trying to parse text for special include statements to pull in specific files.

I have the following function:

function parse_includes($text, $directory = 'includes/') {
preg_match_all('/\[include:([^,]+)\]/', $text, $matches);

foreach($matches[1] as $key => $filename) {
    ob_start();
    include($directory.$filename);
    $include = ob_get_contents();
    ob_end_clean();
    $text = str_replace($matches[0][$key], $include, $text);
}

return $text;

}

In passing in this variable:

$text = 'Below is the footer<br><br>[include:sidebar.php] <br><br> [include:footer.php]<br>';

And echo’ing it:

echo parse_includes($text);

I am getting this error:

Warning: include(includes/sidebar.php] <br><br> [include:footer.php) [function.include]: failed to open stream:

If there is only a single [include: *', it works as expected.

How do I need to modify my REGEX? Note how HTML or white space can surround the brackets on either side.

  • 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-13T00:09:22+00:00Added an answer on June 13, 2026 at 12:09 am

    Regular expressions are greedy by default, meaning they try to match as many characters as possible. As it turns out, that ([^,]+) matches this string:

    sidebar.php] <br><br> [include:footer.php
    

    You can alter your regular expression to use relucant +:

    preg_match_all('/\[include:([^,]+?)\]/', $text, $matches);
    

    This will cause it to match as little as possible, not as much as possible. Alternatively, you can disallow the opening bracket in the matched string:

    preg_match_all('/\[include:([^,[]+)\]/', $text, $matches);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

currently i have the following situation: I'm trying to parse a text, looking for
I have the following text that I am trying to parse: user1@emailaddy1.com <user1@emailaddy1.com>, Jane
I am trying to parse following text in variable... $str = 3,283,518(10,569 / 2,173)
I have a string: [\n['-','some text what\rcontains\nnewlines'],\n\n trying to parse: Regex.Split(@[\n['-','some text what contains
I'm trying to parse some text using PyParser. The problem is that I have
I have a text file which I'm trying to parse. The file looks like
I am trying to parse a text file the contains numeric data. I have
I am trying to parse out a text file that looks like the following:
I am trying to parse a text file with the following structure: latitude 5.0000
I am trying to parse some text using boost. i have looked at the

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.