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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:45:46+00:00 2026-05-12T13:45:46+00:00

Whats is the best way to obtain the content between two strings e.g. ob_start();

  • 0

Whats is the best way to obtain the content between two strings e.g.

ob_start();
include('externalfile.html'); ## see below
$out = ob_get_contents();
ob_end_clean();

preg_match('/{FINDME}(.|\n*)+{\/FINDME}/',$out,$matches);
$match = $matches[0];

echo $match;

## I have used .|\n* as it needs to check for new lines. Is this correct?

## externalfile.html

{FINDME}
Text Here
{/FINDME}

For some reason this appears to work on one place in my code and not another. Am I going about this in the right way? Or is there a better way?

Also is output buffer the way to do this or file_get_contents?

Thanks in advance!

  • 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-12T13:45:46+00:00Added an answer on May 12, 2026 at 1:45 pm
    • Use # instead of / so you dont have to escape them.

    • The modifier s allows . to match newlines.

    • { may be the start of a {n} or {n,m} quantifier. The closing } has no special meaning, but escaping it doesn’t cause an error.

    • The basic

        preg_match('#\{FINDME}(.+)\{/FINDME}#s', $out, $matches);
      
    • The advanced for various tags etc (styling is not so nice by the javascript).

        $delimiter = '#';
        $startTag = '{FINDME}';
        $endTag = '{/FINDME}';
        $regex = $delimiter . preg_quote($startTag, $delimiter) 
                            . '(.*?)' 
                            . preg_quote($endTag, $delimiter) 
                            . $delimiter 
                            . 's';
        preg_match($regex,$out,$matches);
      

    Put this code in a function

    • For any file which you do not want to execue any stray php code, you should use file_get_contents. include/require should not even be an option there.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whats the best/easiest way to obtain a count of items within an IEnumerable collection
What's the best way to pretty-print xml in JavaScript? I obtain xml content through
Whats the best way to check a select box to find out if the
I am wondering what the best way is using php to obtain a list
Whats the best way to detect collisions in a 2d game sprites? I am
Whats the best way to round in VBA Access? My current method utilizes the
whats the best way to export a Datagrid to excel? I have no experience
Whats the best way to separate the string, Parisi, Kenneth into Kenneth and Parisi?
Whats the best way to implement rollover 'buttons' like Stackoverflow has for 'Questions', 'Tags',
So, in your experience, whats the best way? Is there a secure way that's

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.