ST3VO
asked on
Head Delphi Question
Hi all,
I have a hard question here...well, I think it would be.
I need to search through a html file and change any unclosed tags for example:
convert: <br> to <br />
and the ending of <img ... from > to />
Hope someone can help.
thanks
st3vo
I have a hard question here...well, I think it would be.
I need to search through a html file and change any unclosed tags for example:
convert: <br> to <br />
and the ending of <img ... from > to />
Hope someone can help.
thanks
st3vo
ASKER
Yes you are assuming right...I just need to do the self closing tags.
The whole code is in a memo ... so I need some code to search for any <br> <img ... and if there is not / on the closing tag just add it.
Any chance of a sample please?
The whole code is in a memo ... so I need some code to search for any <br> <img ... and if there is not / on the closing tag just add it.
Any chance of a sample please?
ASKER
I guess it first looks for the (eg) <img .... it then has to look for it's ending tag as you could get this:
<a href="#"><img src="bla" alt="bla"></a>
This above would be changed to:
<a href="#"><img src="bla" alt="bla" /></a>
<a href="#"><img src="bla" alt="bla"></a>
This above would be changed to:
<a href="#"><img src="bla" alt="bla" /></a>
Sorry, I don't have Delphi on the machine I'm on now. Good luck with it anyway.
ASKER
Thanks!
ASKER
Anyonec mmmm
ASKER
I mean.... Can anyone help further pls? Us
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wow...great!!! Will check it out as soon as I get home later! thx :o)
ASKER
Perfect!!!! Thanks :o)
I'd read in the whole file as a string. Then use pos( ) to find one of the strings such as '<br'. Unless you're doing this a lot, it'd be simpler to do one at a time. Then step through the string from there. Start a counter at zero and increment it when you find a '<', decrement it when you find a '>' unless the counter is zero, then it's the close tag. Write out to the output file everything from the beginning of the string to right before the close tag. Write out the ' />', delete the beginning part of the string, and repeat until all the tags are found (and write out the rest of the string).
Then reread the file to do the '<img', etc.
If you wanted to do the enclosing tags like '<p>' it would be possible but a bit more complicated. You'd have go through the string making a stack of the opening tags as you encounter them, then when you find a closing tag, if it matches pop it off the stack, otherwise add the missing end tag and try matching the next thing on the stack.