Asked  7 Months ago    Answers:  5   Viewed   34 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.
Wednesday, March 31, 2021
answered 7 Months ago

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];
Wednesday, March 31, 2021
answered 7 Months ago

file_get_contents() will get the content of a file, not execute it as a PHP script. If you want this piece of code to be executed, you need to either include it, or process it (through an HTTP request to Apache, for instance).

If you include this file, it'll be processed as PHP code, and of course, print your HTML tags (include* can take any kind of file).

If you need to work with its content before you print it, use ob_* functions to manipulate the PHP output buffer. See :

ob_start(); // Start output buffer capture.
include("yourtemplate.php"); // Include your template.
$output = ob_get_contents(); // This contains the output of yourtemplate.php
// Manipulate $output...
ob_end_clean(); // Clear the buffer.
echo $output; // Print everything.

By the way, such mechanism sounds heavy for a template engine. Basically, templates should not contain PHP code. If you want such behavior, have a look at Twig :

Wednesday, March 31, 2021
answered 7 Months ago

$cache_disqus is defined outside your function, and is therefore not accessible within the function.

Check out the PHP documentation on variable scope.

Saturday, May 29, 2021
answered 5 Months ago

Try this:

test.match(new RegExp(firstvariable + "(.*)" + secondvariable));
Monday, August 9, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :