Asked  7 Months ago    Answers:  5   Viewed   34 times

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!

 Answers

86
  • 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

    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.
Wednesday, March 31, 2021
 
mcography
answered 7 Months ago
61

You can just explode it:

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

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

echo $display[1];
// prints 234

Or, use preg_match

<?php
$string = 'reply-234-private';
if (preg_match('/reply-(.*?)-private/', $string, $display) === 1) {
    echo $display[1];
}
Wednesday, March 31, 2021
 
twk
answered 7 Months ago
twk
46

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 : http://www.php.net/manual/fr/ref.outcontrol.php

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 : http://twig.sensiolabs.org/

Wednesday, March 31, 2021
 
dkcwd
answered 7 Months ago
14

$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
 
IvanH
answered 5 Months ago
80

Try this:

test.match(new RegExp(firstvariable + "(.*)" + secondvariable));
Monday, August 9, 2021
 
HPJM
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 :