Free Pascal Strip Only Some HTML 字符串中的标签
Free Pascal Strip Only Some HTML Tags from a string
我想知道是否可以从字符串中去除特定的 html 标签。
我只想去除以 <img>
开头的标签。但是必须删除所有 <img ...>
内容。这是因为我需要从字符串中删除图像。
我尝试过改编这个套路:
function StripHTML(S: string): string;
var
TagBegin, TagEnd, TagLength: integer;
begin
TagBegin := Pos( '<', S); // search position of first <
while (TagBegin > 0) do begin // while there is a < in S
TagEnd := Pos('>', S); // find the matching >
TagLength := TagEnd - TagBegin + 1;
Delete(S, TagBegin, TagLength); // delete the tag
TagBegin:= Pos( '<', S); // search for next <
end;
Result := S; // give the result
end;
这样(换两行):
TagBegin := Pos( '<img', S); // search position of first <
...
TagBegin:= Pos( '<img', S); // search for next <
但是代码陷入了一个牢不可破的循环。 :(
我应用了@Abelisto 的技巧,现在可以使用了。
这是代码(我必须引用原始代码是在这里找到的:
http://www.festra.com/eng/snip12.htm)
function StripHTML(S: string): string;
var
TagBegin, TagEnd : integer;
begin
TagBegin := Pos( '<img', S); // search position of first <
while (TagBegin > 0) do begin // while there is a < in S
TagEnd := PosEx('>', S, TagBegin); // find the matching >
Delete(S, TagBegin, (TagEnd - TagBegin) + 1); // delete the tag
TagBegin:= Pos( '<img', S); // search for next <
end;
Result := S; // give the result
end;
我想知道是否可以从字符串中去除特定的 html 标签。
我只想去除以 <img>
开头的标签。但是必须删除所有 <img ...>
内容。这是因为我需要从字符串中删除图像。
我尝试过改编这个套路:
function StripHTML(S: string): string;
var
TagBegin, TagEnd, TagLength: integer;
begin
TagBegin := Pos( '<', S); // search position of first <
while (TagBegin > 0) do begin // while there is a < in S
TagEnd := Pos('>', S); // find the matching >
TagLength := TagEnd - TagBegin + 1;
Delete(S, TagBegin, TagLength); // delete the tag
TagBegin:= Pos( '<', S); // search for next <
end;
Result := S; // give the result
end;
这样(换两行):
TagBegin := Pos( '<img', S); // search position of first <
...
TagBegin:= Pos( '<img', S); // search for next <
但是代码陷入了一个牢不可破的循环。 :(
我应用了@Abelisto 的技巧,现在可以使用了。 这是代码(我必须引用原始代码是在这里找到的: http://www.festra.com/eng/snip12.htm)
function StripHTML(S: string): string;
var
TagBegin, TagEnd : integer;
begin
TagBegin := Pos( '<img', S); // search position of first <
while (TagBegin > 0) do begin // while there is a < in S
TagEnd := PosEx('>', S, TagBegin); // find the matching >
Delete(S, TagBegin, (TagEnd - TagBegin) + 1); // delete the tag
TagBegin:= Pos( '<img', S); // search for next <
end;
Result := S; // give the result
end;