delphi - removing duplicated text inside a string -


i have string got text this

str := 'hi there name vlark , images <img src=""><img src=""> images need controlled <img src=""><img src=""><img src=""><img src="">';  

this string have 6 images tags <img wanted control on tags if string have more 3 images tags leave first 3 , remove rest image tags . couldnt figure out how can in coding

strategy:

  • find position , length of complete enclosed tags: <img , >
  • if count larger 3, remove tag.

function removeexcessivetags( const s: string): string; var   tags,cp,p : integer; begin   tags := 0;   cp := 1;   result := s;   repeat     cp := pos('<img',result,cp);     if (cp > 0) begin      // find end of tag       p := pos('>',result,cp+4);       if (p > 0) begin         inc(tags);         if (tags > 3) begin // delete tag if more 3 tags           delete(result,cp,p-cp+1);         end         else           cp := p+1;  // next search start position       end       else         cp := 0;  // reached end of string, abort search     end;   until (cp = 0); end; 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -