?
Solved

control image tags in a string ?

Posted on 2016-11-10
12
Medium Priority
?
194 Views
Last Modified: 2016-11-20
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
Comment
Question by:dolphin King
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 41883449
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
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 41883630
@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
 

Author Comment

by:dolphin King
ID: 41884413
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
Technology Partners: 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!

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 41885564
how do those tags get in that text ?
where is the source of those texts coming from ?
0
 

Author Comment

by:dolphin King
ID: 41885899
@Geert Gruwez those tags are inserted from Tedit control so they can be changeable and not only images
0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 41886135
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
 
LVL 23

Expert Comment

by:Ferruccio Accalai
ID: 41894445
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
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 41894620
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
 

Author Comment

by:dolphin King
ID: 41894631
@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
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 2000 total points
ID: 41894714
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
 

Author Closing Comment

by:dolphin King
ID: 41894719
Thats works Thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question