Replace between 2 objects?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Replace between 2 objects?

Post by Mr. Tech »

Hi!

Is there a way to replace text between 2 objects? E.G:

Code: Select all

<!-- START Replace -->
This is what you replace!
<!-- END Replace -->
So what I want to do is to be able to replace what's between START and END Replace. What code would I need to do that?

Do you understand?

Thanks
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

Anyone?
User avatar
detrox
Forum Newbie
Posts: 21
Joined: Wed Jun 04, 2003 1:27 am
Location: P.R.China

Post by detrox »

Maybe you can use fopen() and fwrite() to access this file. To find the begin tag and end tag then get the inner text.

Is this what you want?
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

If the 'tags' are the onlything on 1 line, use file.

If it's not, i let somebody els do the f* job :)

PS: I hate working with file's. I let yapter do all the html stuff.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Sounds like a job for preg_replace.
User avatar
bradvan
Forum Newbie
Posts: 10
Joined: Mon Jun 02, 2003 8:26 pm
Location: Rylstone, Australia
Contact:

Post by bradvan »

When you say that you want to replace the text, do you mean you want it to be dynamic as in a variable or do you just want to straight search a file and replace the text with different static text?

If you want the text to be dynamic (that is change depending on user requests) then you could replace the text with a php variable e.g $text, and define $text depending on some condition.

e.g:

<?php

if ($_GET["request"] == "this")
{
$text = "add this text";
}

else $text = "add this text instead";
?>

<!-- start replace -->
<? print "$text"; ?>
<!-- end replace -->


Is this the sort of thing you are talking about?
Post Reply