Forum Moderators: coopster

Message Too Old, No Replies

Get contents between two markers

         

sampford

1:51 pm on Jan 24, 2007 (gmt 0)

10+ Year Member



I am trying to build a web based php editor for myself to make editing pages easy. Most of the various bits and pieces I can easily include, by including varibles into text input fields. Just one problem remains, how to edit PHP in it?

What I thought of doing was to use something like strpos of two different tags;


ob_start();
require_once("index.php");
$buffer = ob_get_contents();
ob_end_clean();

$pos1 = strpos($buffer, "//extra php here");
$pos2 = strpos($buffer, "//end extra php");

However this does not seem to work - maybe it is because there are spaces in the string - but how can I get the positions of these two markers in the $buffer - or is there a better way of doing this?

I would also need something to get rid of everything at the beginning up until the marker, and everything after the 2nd marker.

The only thing I have thought of, is to perhaps create another file to contine the extra PHP code in - but why increase used disk space, whilst also possibly letting the users see source code...

... if anyone could perhaps suggest a better idea, or a solution - I would be most greatful!

Thanks!

justageek

3:38 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could try explode or split. The functions split text into an array.

// Find your starting point delimiter in the original text
$array1 = explode(delimiter,$text);
// Find the ending point delimiter in the array
$array2 = explode(delimiter,$array1[1]);

$array2[0] should have the value you are searching for.

JAG

henry0

3:50 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is your content DB originated?
if so re-export any content in a text area
or even better in an added editor
then edit
and reload it

cmarshall

4:07 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why bother with the ob stuff?

Just do what I do. Use file_get_contents() or file().

That gives you the whole schmiel in an array or a string, to do with as you please.

I actually do almost exactly what you are talking about, except I use it to parse a Dreamweaver template.

sampford

5:14 pm on Jan 24, 2007 (gmt 0)

10+ Year Member



Thanks guys - I looked into each part with in a little more details, and got the below to work;

<?

$buffer = implode('', file('index.php'));

$array1 = explode("//extra php start",$buffer);
$array2 = explode("//extra php end",$array1[1]);

$contents = $array2[0];

echo "<textarea cols=50 rows=10>$contents</textarea>";

?>

I'm sure i've tried to use file() beofre but somehow it did not work, I also check in the PHP manual and it suggested the first line... and it works, and now I happy :)

Thanks again!

Edit: Just one more thing - on the same page, is there a way to extract the contents of a varible (outside of these tags) without including the page? For example;

//extra php start
$stuff = "bla bla bla";
//extra php end
$title = "Interesting page";

If I want to include the $title varible into an input field to be edited, what is the best way to go about extracting it? I know it can be done by including the page, or using the ob_start() thing - but is there a better way?

Thanks!

cmarshall

5:31 pm on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$buffer = implode('', file('index.php'));

This is exactly the same as:

$buffer = [url=http://us3.php.net/manual/en/function.file-get-contents.php]file_get_contents[/url]('index.php');

As to the other thing, what I do for things like that is declare a small include file with various variable settings, and declare them as global. I then include this small file in the various files that use the variables, and I don't have to worry about including the big kahuna.