Forum Moderators: coopster
So it has to:
- read start tag which is <!-- ANDedit -->
- read stop tag which is <!-- STOP_ANDedit -->
- take text which is in between the two tags and place it in a
text area.
- then save the text back into the same location on the html page?
any ideas?
Try using strpos() to find your start tag, then using that position as the start position again with strpos() to find your stop tag.
<?php
$Start = strpos($HTML, "<!-- ANDedit -->");
$Length = strpos($HTML, "<!-- STOP_ANDedit -->", $Start);
$Text = substr($HTML, $Start, $Length);
?>
That'll have to be cleaned up a little to remove your tags from $Text. But the idea here is that you keep doing this until you get to the end of $HTML, and keep starting where the previous iteration left off.
In your example, $Start should equal 57 (I counted by hand, so I could be wrong), and let's change my $Length calculator from before to:
$Length = strpos($HTML, "<!-- STOP_ANDedit -->", $Start) - $Start;
That means $Length should equal 27 (84 - 57). When I get the substr, it should return:
<!-- ANDedit -->
Editable
Like I said, it will definately need to be cleaned up a bit. Perhaps instead of starting your substr() at $Start, it could be started at $Start + strlen("<!-- ANDedit -->").
String manipulation like this always takes me a lot of tweaking to get right. I started out with the results that you're getting, then I tweak it and may be off by a character or two, and then tweak it some more to finally get it right :-).
<how would i place the changed or edited text back into the html file in the same position?>
You could try putting the contents of the edited text into one variable, setting a new variable to the value of the text before the first tag, setting a new variable to the value of the text after the last tag, then concatenating them all together.
Use the same method as you did before to find the start tag. But instead of taking the text after it, take the text before it and assign it to a variable, say $Left.
Then, use the same method to find the end tag. Instead of using it as an ending marker, use it as a beginning marker and take everything after it, and assign it to a variable, say $Right.
Assign your modified text (the stuff that was between the two tags) to $Modified.
Next, paste them all together:
$NewText = $Left . $Modified . $Right
Now $NewText contains all of the text but with the new stuff modified.
You could probably also do the same thing with str_replace() where $Text is your unedited text and $HTML is you entire original text (from the previous examples):
$NewText = ("<!-- ANDedit -->" . $Text . "<!-- STOP_ANDedit -->", $Modified, $HTML);
This will replace the unmodified text and the tags with the modified text.