Viewed   83 times

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

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

$match = $matches[0];

echo $match;

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

## externalfile.html

Text Here

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!


  • Use # instead of / so you dont have to escape them.
  • The modifier s makes . and s also include newlines.
  • { and } has various functionality like from n to m times in {n,m}.
  • The basic

  • 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';

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.
Sunday, October 16, 2022

A few years ago I benchmarked the two and CURL was faster. With CURL you create one CURL instance which can be used for every request, and it maps directly to the very fast libcurl library. Using file_get_contents you have the overhead of protocol wrappers and the initialization code getting executed for every single request.

I will dig out my benchmark script and run on PHP 5.3 but I suspect that CURL will still be faster.

Friday, December 16, 2022

You can just explode it:

$string = 'reply-234-private';
$display = explode('-', $string);

// prints array(3) { [0]=> string(5) "reply" [1]=> string(3) "234" [2]=> string(7) "private" }

echo $display[1];
// prints 234

Or, use preg_match

$string = 'reply-234-private';
if (preg_match('/reply-(.*?)-private/', $string, $display) === 1) {
    echo $display[1];
Friday, October 7, 2022

In your url try:


(append whatever the rest of the URL for your API should be)

Friday, November 4, 2022

Your RegEx is definitely incorrect. Look what it evaluates to:

/[^<!-- test4 Begin ColdFusion Template Html_Head -->](.*)[^<!-- test4 End ColdFusion Template Html_Head -->]/

This is wrong; brackets in RegEx denote a character set, not a literal string. Furthermore, adding the caret ^ symbol negates the character set, meaning essentially "none of these characters".

If you want to search for a literal, just use those characters:

$tagSearch = '/'. $tagBegin .'(.*)'. $tagEnd .'/';

Also, I would make the wildcard lazy by adding a ? so it doesn't potentially match other tags in your code:

$tagSearch = '/'. $tagBegin .'(.*?)'. $tagEnd .'/';

Finally, it sounds like you're trying to actually modify the file itself. To do that, you'll need to write your modified data back to the file. Changing the data in-memory will not automatically save those changes to the file on disk.

Wednesday, November 2, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :