[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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

0
ST3VO
Asked:
ST3VO
  • 7
  • 2
1 Solution
 
SuperdaveCommented:
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.  
0
 
ST3VOAuthor Commented:
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?

 
0
 
ST3VOAuthor Commented:
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>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
SuperdaveCommented:
Sorry, I don't have Delphi on the machine I'm on now.  Good luck with it anyway.
0
 
ST3VOAuthor Commented:
Thanks!
0
 
ST3VOAuthor Commented:
Anyonec mmmm
0
 
ST3VOAuthor Commented:
I mean.... Can anyone help further pls? Us
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
This one should do wath you need.
If you have to add other tags just add then into the array
var
  Form1: TForm1;
(*note that to initialize a variable it must be a global variable, so we declare our array here.
If you need you could add other tags to this array *)
  Tags: array [0..1] of String = ('<br', '<img');

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  s: String;
  i, t, CaretPos: integer;
begin
  for t := low(Tags) to high(Tags) do
  begin
    s := Memo1.Text; //get the text from Memo
    CaretPos := 0;
    while pos(Tags[t], s) > 0 do
    begin
      i := pos(Tags[t], s); //found our tag
      while s[i] <> '>' do
        inc(i); //let's search > starting from here
      Memo1.SelStart := i-1+CaretPos; //this sets the position in memo where we'll insert
      Memo1.SelText := ' /';  //let's insert our need
      delete(s, 1, i); //refresh our string text to step over with next search
      CaretPos := CaretPos+i+2;  //refresh our new position in memo for next insertion
    end;
  end;
end;

Open in new window

0
 
ST3VOAuthor Commented:
Wow...great!!! Will check it out as soon as I get home later! thx :o)
0
 
ST3VOAuthor Commented:
Perfect!!!! Thanks :o)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now