Viewed   83 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

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

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
 
1

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];
}
Friday, October 7, 2022
3

In your url try:

http://user:password@site/ 

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

Friday, November 4, 2022
 
matoran
 
4

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 :