Forum Moderators: coopster

Message Too Old, No Replies

Extracting text between certain tags?

Not sure at alll

         

Able Net Design

3:06 am on Oct 31, 2003 (gmt 0)

10+ Year Member



basically what i am doing is building a php app which allows users to open up html files and in between the certain tags edit the contents.

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?

NickCoons

7:15 am on Oct 31, 2003 (gmt 0)

10+ Year Member



Able_Net_Design,

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.

Able Net Design

7:57 am on Oct 31, 2003 (gmt 0)

10+ Year Member



I kinda works

<?php
$Start = strpos($HTML, "<!-- ANDedit -->");
$Length = strpos($HTML, "<!-- STOP_ANDedit -->", $Start);
$Text = substr($HTML, $Start, $Length);
?>

it finds that start tag however it doesnt find the stop tag so it continues to print out all of the html and text after that.

NickCoons

8:30 am on Oct 31, 2003 (gmt 0)

10+ Year Member



Able_Net_Design,

<$Text = substr($HTML, $Start, $Length);>

Do you mean that $Length is too large and it gets too much of the string, or that $Length ends up being false?

If the former, try "$Length - $Start" in place of "$Length".

Able Net Design

8:50 am on Oct 31, 2003 (gmt 0)

10+ Year Member



i mean if the html file is

<html>
<head><title>Demo</title>
</head>
<body>
bla bla
<!-- ANDedit -->
Editable
<!-- STOP_ANDedit -->

</body>
</html>

it will print out:

<!-- ANDedit -->
Editable
<!-- STOP_ANDedit -->

</body>
</html>

the closing body and html tags.

NickCoons

4:37 pm on Oct 31, 2003 (gmt 0)

10+ Year Member



Able_Net_Design,

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 :-).

Able Net Design

12:34 am on Nov 3, 2003 (gmt 0)

10+ Year Member



ok i have managed to get that to work.... but how would i place the changed or edited text back into the html file in the same position?

NickCoons

5:52 am on Nov 3, 2003 (gmt 0)

10+ Year Member



Able Net Design,

<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.

Able Net Design

9:43 am on Nov 9, 2003 (gmt 0)

10+ Year Member



being a newbie to php i wouldn't have a clue on how to go about this..

NickCoons

9:25 pm on Nov 9, 2003 (gmt 0)

10+ Year Member



Able Net Design,

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.