• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

control image tags in a string ?

i have a string that contain those 6 image tags

StringI := '<img src="'path'"><img src="'path'"><img src="'path'"><img src="'path'"><img src="'path'"><img src="'path'">';

Open in new window


i want to control on that string so if the images string longer than or equal 6 remove the last 3 image tags from string  .

how can i achieve that in coding
0
dolphin King
Asked:
dolphin King
  • 4
  • 4
  • 2
  • +1
1 Solution
 
Pierre CorneliusCommented:
I suggest you break it down into an array of strings giving you indexed access to them. e.g.
uses StrUtils, Types;

procedure TForm1.Button1Click(Sender: TObject);
var StringI: string;
    i: integer;
    s: TStringDynArray;
begin
  StringI:= '<img src="path"><img src="path"><img src="path"><img src="path"><img src="path"><img src="path">';
  StringI:= StringReplace(StringI, '><','>;<', [rfReplaceAll]);
  s:= SplitString(StringI, ';');
  for i:= 0 to length(s)-1
    do ListBox1.Items.Add(s[i]);
end;

Open in new window

0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
@i want to control on that string so if the images string longer than or equal 6 remove the last 3 image tags from string  .

Do you mean that you need just the first 3 img tags from the string?
If so you could use a function like this:
uses StrUtils;
function FirstThreeImgTags(S: String): string;
var
  p, t, i: integer;
begin
  p := 0;
  i := 1;
  while i <= 3 do
  begin
    p := posex('>', S, p+1);
    if p = 0 then
      break
    else
      t := p;
    inc(i);
  end;
  result := copy(S, 1, t);
end;

Open in new window

0
 
dolphin KingAuthor Commented:
this text will be dynamic as example
strsm := 'Hi there My name is Trunk and this is my images <img src=""><img src=""> But This images need to be controlled <img src=""><img src=""><img src=""><img src="">'; 

Open in new window


i need to control on how Manny images Tags on that string

@Ferruccio Accalai your code clear all the text even without images on it  also i use this method in dll application not exe

@Pierre Cornelius This cannot be array its changeable string it does not have stable text or identification
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Geert GruwezOracle dbaCommented:
how do those tags get in that text ?
where is the source of those texts coming from ?
0
 
dolphin KingAuthor Commented:
@Geert Gruwez those tags are inserted from Tedit control so they can be changeable and not only images
0
 
Geert GruwezOracle dbaCommented:
So your form is used to generate the string
Well, do the checks upon generating the string
Checking after with parsing is more difficult
0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Why a deletion request without giving more inputs?
In the first question the request was about something that seems different from the real needs.
It's hard to give the right answer to a not completely clear question.
0
 
Geert GruwezOracle dbaCommented:
our answers are not helpful because you are not responding to our questions
this will be the same for every question you ask here

you didn't post any of your code either

so we are second guessing at what's really going on
which, btw, you didn't explain either
0
 
dolphin KingAuthor Commented:
@Ferruccio Accalai  i have gives you more details see the following quote



this text will be dynamic as example
strsm := 'Hi there My name is Trunk and this is my images <img src=""><img src=""> But This images need to be controlled <img src=""><img src=""><img src=""><img src="">';

Open in new window

i need to control on how Manny images Tags on that string

@Ferruccio Accalai your code clear all the text even without images on it  also i use this method in dll application not exe

@Pierre Cornelius This cannot be array its changeable string it does not have stable text or identification



@Geert Gruwez giving more explanation already posted above ! which kind of codes i have to post ? the question were about how to do it.  that's means i am unable to write a solution myself .
0
 
Geert GruwezOracle dbaCommented:
well, in pseudo it would be this:

using regular expression (cos a simple search doesn't work) count for the number of <img  src="">
and replace from the 7th with nothing, that's also with regex

the regex pattern you need to find is this: (case insensitive)
<img.*?src=".*?".*?>

this replaces all items found beyond the 6'th item with an empty string
unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, RegularExpressions;

type
  TForm1 = class(TForm)
    mmoInput: TMemo;
    mmoOutput: TMemo;
    btnParse: TButton;
    procedure btnParseClick(Sender: TObject);
  private
    nMatchCount: Integer;
    function MatchReplace(const Match: TMatch): string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnParseClick(Sender: TObject);
var R: TRegEx;
  mc: TMatchCollection;
  n: Integer;
begin
  // Find entry <img src="">
  // count
  R.Create('<img.*?src=".*?".*?>');
  mc := R.Matches(mmoInput.Text);
  n := mc.Count;
  if n > 6 then
  begin
    nMatchCount := 0;
    mmoOutput.Text := R.Replace(mmoInput.Text, MatchReplace);
  end;
end;

function TForm1.MatchReplace(const Match: TMatch): string;
begin
  Inc(nMatchCount);
  Result := Match.Value;
  if nMatchCount> 6 then Result := '';
end;

end.

Open in new window

0
 
dolphin KingAuthor Commented:
Thats works Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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