Link to home
Start Free TrialLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

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

Avatar of Superdave
Superdave
Flag of United States of America image

First I'll assume you have a list of the tags that need the / added, rather than finding any unclosed ones.  Besides, you might have unclosed <p> tags for example but you would have to deal with those differently anyway, so I take it you're just dealing with the self-closing ones (<br />, <img ... /> and <hr /> also comes to mind).

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.  
Avatar of 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?

 
Avatar of ST3VO

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>
Sorry, I don't have Delphi on the machine I'm on now.  Good luck with it anyway.
Avatar of ST3VO

ASKER

Thanks!
Avatar of ST3VO

ASKER

Anyonec mmmm
Avatar of ST3VO

ASKER

I mean.... Can anyone help further pls? Us
ASKER CERTIFIED SOLUTION
Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ST3VO

ASKER

Wow...great!!! Will check it out as soon as I get home later! thx :o)
Avatar of ST3VO

ASKER

Perfect!!!! Thanks :o)