Trong bài đăng này, chúng tôi sẽ trình bày cách xóa tất cả các thẻ HTML khỏi một chuỗi mà không biết những thẻ nào được bao gồm bên trong chuỗi đó
Có nhiều cách để đạt được nhiệm vụ này, nhưng không có cách nào đảm bảo bạn xóa tất cả các thẻ. Chúng ta sẽ xem xét một số phương pháp của nó
public static string StripHTML[string input]
{
return Regex.Replace[input, "", String.Empty];
}
Hàm này truyền một tham số chuỗi và chúng tôi sử dụng hàm
var doc = new HtmlDocument[];
doc.LoadHtml[d];
3 của var doc = new HtmlDocument[];
doc.LoadHtml[d];
4 để xóa các thẻ vì chữ ký của các thẻ được cung cấp trong đầu vào hàmNó không hoạt động cho tất cả các trường hợp, nhưng hầu hết đều hoạt động tốt. Bạn sẽ cần viết thuật toán để xóa tất cả các thẻ khỏi đầu vào chuỗi
Một giải pháp khác là sử dụng Gói Agility HTML
internal static string RmvTags[string d]
{
if[string.IsNullOrEmpty[d]]
return string.Empty;
var doc = new HtmlDocument[];
doc.LoadHtml[d];
var accTags = new String[] { "strong", "em", "u"};
var n = new Queue[doc.DocumentNode.SelectNodes["./*|./text[]"]];
while[n.Count > 0]
{
var no = nodes.Dequeue[];
var pNo = no.ParentNode;
if[!accTags.Contains[no.Name] && no.Name != "#text"]
{
var cNo = no.SelectNodes["./*|./text[]"];
if [cNo != null]
{
foreach [var c in cNo]
{
n.Enqueue[c];
pNo.InsertBefore[c, no];
}
}
pNo.RemoveChild[no];
}
}
return doc.DocumentNode.InnerHtml;
}
Điều này sẽ hoạt động tốt ngoại trừ các nút văn bản thô
var doc = new HtmlDocument[];
doc.LoadHtml[d];
5, var doc = new HtmlDocument[];
doc.LoadHtml[d];
6, var doc = new HtmlDocument[];
doc.LoadHtml[d];
7 và văn bản thô. Hàm này lấy một chuỗi làm tham số trong biến var doc = new HtmlDocument[];
doc.LoadHtml[d];
8Dòng
var doc = new HtmlDocument[];
doc.LoadHtml[d];
9 kiểm tra xem chuỗi đã trống chưa thì trả về chuỗi trốngvar doc = new HtmlDocument[];
doc.LoadHtml[d];
Các câu lệnh này tạo một tài liệu HTML mới và tải dữ liệu vào tài liệu. Nó đã là một chuỗi thẻ HTML và sẽ tuân theo mẫu HTML
Dòng
internal static string RmvTags[string d]
{
if[string.IsNullOrEmpty[d]]
return string.Empty;
var doc = new HtmlDocument[];
doc.LoadHtml[d];
var accTags = new String[] { "strong", "em", "u"};
var n = new Queue[doc.DocumentNode.SelectNodes["./*|./text[]"]];
while[n.Count > 0]
{
var no = nodes.Dequeue[];
var pNo = no.ParentNode;
if[!accTags.Contains[no.Name] && no.Name != "#text"]
{
var cNo = no.SelectNodes["./*|./text[]"];
if [cNo != null]
{
foreach [var c in cNo]
{
n.Enqueue[c];
pNo.InsertBefore[c, no];
}
}
pNo.RemoveChild[no];
}
}
return doc.DocumentNode.InnerHtml;
}
0 cho biết thẻ nào được phép. Bạn có thể thay đổi, thêm bớt các thẻ theo yêu cầu của mìnhSau đó, trong vòng lặp
internal static string RmvTags[string d]
{
if[string.IsNullOrEmpty[d]]
return string.Empty;
var doc = new HtmlDocument[];
doc.LoadHtml[d];
var accTags = new String[] { "strong", "em", "u"};
var n = new Queue[doc.DocumentNode.SelectNodes["./*|./text[]"]];
while[n.Count > 0]
{
var no = nodes.Dequeue[];
var pNo = no.ParentNode;
if[!accTags.Contains[no.Name] && no.Name != "#text"]
{
var cNo = no.SelectNodes["./*|./text[]"];
if [cNo != null]
{
foreach [var c in cNo]
{
n.Enqueue[c];
pNo.InsertBefore[c, no];
}
}
pNo.RemoveChild[no];
}
}
return doc.DocumentNode.InnerHtml;
}
1, nó sử dụng hàng đợi để thêm tất cả các nút tài liệu, loại bỏ từng nút và xóa thẻ HTMLQuá trình tiếp tục cho đến khi tất cả dữ liệu được làm sạch và sau đó nó trả về HTML bên trong của tài liệu HTML, vốn đã là một văn bản được làm sạch
Như đã nói trước đó, không có quy tắc hay phương pháp cứng nhắc và nhanh chóng nào để đạt được nhiệm vụ này. Có nhiều cách, và không có cách nào là hoàn toàn đáng tin cậy
Hôm nọ tôi thấy một câu hỏi đơn giản nhưng thú vị trên internet. Ai đó đã đăng muốn biết. “Làm cách nào để xóa các thẻ HTML trong C?”
Nó nhanh chóng xuất hiện trong đầu tôi, nhưng với
Nếu bạn hiểu với nó thực sự rất dễ dàng, chỉ cần
- Bao gồm tiêu đề
0;internal static string RmvTags[string d] { if[string.IsNullOrEmpty[d]] return string.Empty; var doc = new HtmlDocument[]; doc.LoadHtml[d]; var accTags = new String[] { "strong", "em", "u"}; var n = new Queue[doc.DocumentNode.SelectNodes["./*|./text[]"]]; while[n.Count > 0] { var no = nodes.Dequeue[]; var pNo = no.ParentNode; if[!accTags.Contains[no.Name] && no.Name != "#text"] { var cNo = no.SelectNodes["./*|./text[]"]; if [cNo != null] { foreach [var c in cNo] { n.Enqueue[c]; pNo.InsertBefore[c, no]; } } pNo.RemoveChild[no]; } } return doc.DocumentNode.InnerHtml; }
- Thông báo mẫu của biểu thức chính quy;
- Và cuối cùng dùng hàm
1 để thay thế bằng chuỗi bạn muốninternal static string RmvTags[string d] { if[string.IsNullOrEmpty[d]] return string.Empty; var doc = new HtmlDocument[]; doc.LoadHtml[d]; var accTags = new String[] { "strong", "em", "u"}; var n = new Queue[doc.DocumentNode.SelectNodes["./*|./text[]"]]; while[n.Count > 0] { var no = nodes.Dequeue[]; var pNo = no.ParentNode; if[!accTags.Contains[no.Name] && no.Name != "#text"] { var cNo = no.SelectNodes["./*|./text[]"]; if [cNo != null] { foreach [var c in cNo] { n.Enqueue[c]; pNo.InsertBefore[c, no]; } } pNo.RemoveChild[no]; } } return doc.DocumentNode.InnerHtml; }
Tóm lại code là thế này
#include
#include
int main[]{
std::string html = "This is a link";
std::regex tags["]*>"];
std::string remove{};
std::cout 0]
{
var no = nodes.Dequeue[];
var pNo = no.ParentNode;
if[!accTags.Contains[no.Name] && no.Name != "#text"]
{
var cNo = no.SelectNodes["./*|./text[]"];
if [cNo != null]
{
foreach [var c in cNo]
{
n.Enqueue[c];
pNo.InsertBefore[c, no];
}
}
pNo.RemoveChild[no];
}
}
return doc.DocumentNode.InnerHtml;
}
4Để biết thêm thông tin, hãy truy cập trang POSIX của sách hướng dẫn bằng lệnh
man regex.h
Xóa THẺ HTML trong CSau khi bạn kiểm tra xem một chuỗi đã cho có thẻ hay không [lưu xử lý], bước tiếp theo là xóa thẻ
Tôi đã nghĩ ra một giải pháp của riêng mình [và đơn giản 💡 ] mà những người yêu thích C có thể phản đối, nhưng nó hiệu quả 😎. Bản thân mã là
- Bao gồm tiêu đề
5 để sử dụnginternal static string RmvTags[string d] { if[string.IsNullOrEmpty[d]] return string.Empty; var doc = new HtmlDocument[]; doc.LoadHtml[d]; var accTags = new String[] { "strong", "em", "u"}; var n = new Queue[doc.DocumentNode.SelectNodes["./*|./text[]"]]; while[n.Count > 0] { var no = nodes.Dequeue[]; var pNo = no.ParentNode; if[!accTags.Contains[no.Name] && no.Name != "#text"] { var cNo = no.SelectNodes["./*|./text[]"]; if [cNo != null] { foreach [var c in cNo] { n.Enqueue[c]; pNo.InsertBefore[c, no]; } } pNo.RemoveChild[no]; } } return doc.DocumentNode.InnerHtml; }
0;#include #include int main[]{ regex_t regex; int check_regex = regcomp[®ex, "]*>", REG_EXTENDED]; check_regex = regexec[®ex, "
Tag
", 0, NULL, 0]; !check_regex ? printf["Has tags!\n"] : printf["It has no tags.\n"]; regfree[®ex]; return 0; }
1 để sử dụng#include #include int main[]{ regex_t regex; int check_regex = regcomp[®ex, "]*>", REG_EXTENDED]; check_regex = regexec[®ex, "
Tag
", 0, NULL, 0]; !check_regex ? printf["Has tags!\n"] : printf["It has no tags.\n"]; regfree[®ex]; return 0; }
2;#include #include int main[]{ regex_t regex; int check_regex = regcomp[®ex, "]*>", REG_EXTENDED]; check_regex = regexec[®ex, "
Tag
", 0, NULL, 0]; !check_regex ? printf["Has tags!\n"] : printf["It has no tags.\n"]; regfree[®ex]; return 0; }- và
3 để sử dụng loại#include #include int main[]{ regex_t regex; int check_regex = regcomp[®ex, "]*>", REG_EXTENDED]; check_regex = regexec[®ex, "
Tag
4#include #include int main[]{ regex_t regex; int check_regex = regcomp[®ex, "]*>", REG_EXTENDED]; check_regex = regexec[®ex, "
Tag
", 0, NULL, 0]; !check_regex ? printf["Has tags!\n"] : printf["It has no tags.\n"]; regfree[®ex]; return 0; }
- Xác định hằng số
5 để tối ưu hóa hiệu suất#include #include int main[]{ regex_t regex; int check_regex = regcomp[®ex, "]*>", REG_EXTENDED]; check_regex = regexec[®ex, "
Tag
", 0, NULL, 0]; !check_regex ? printf["Has tags!\n"] : printf["It has no tags.\n"]; regfree[®ex]; return 0; } - Tạo hàm trả về
6 để xác định lại. Và chức năng đó như sau#include #include int main[]{ regex_t regex; int check_regex = regcomp[®ex, "]*>", REG_EXTENDED]; check_regex = regexec[®ex, "
Tag
", 0, NULL, 0]; !check_regex ? printf["Has tags!\n"] : printf["It has no tags.\n"]; regfree[®ex]; return 0; }- Tôi đã chèn một vòng lặp
7 để đi qua chuỗi theo số lượng ký tự trong đó;#include #include int main[]{ regex_t regex; int check_regex = regcomp[®ex, "]*>", REG_EXTENDED]; check_regex = regexec[®ex, "
Tag
", 0, NULL, 0]; !check_regex ? printf["Has tags!\n"] : printf["It has no tags.\n"]; regfree[®ex]; return 0; } - Nó kiểm tra xem ký tự mở của thẻ
8 có được xác định trong chuỗi hay không;#include #include int main[]{ regex_t regex; int check_regex = regcomp[®ex, "]*>", REG_EXTENDED]; check_regex = regexec[®ex, "
Tag
", 0, NULL, 0]; !check_regex ? printf["Has tags!\n"] : printf["It has no tags.\n"]; regfree[®ex]; return 0; } - Nếu có, nó biến biến boolean
9 thành#include #include int main[]{ regex_t regex; int check_regex = regcomp[®ex, "]*>", REG_EXTENDED]; check_regex = regexec[®ex, "
Tag
", 0, NULL, 0]; !check_regex ? printf["Has tags!\n"] : printf["It has no tags.\n"]; regfree[®ex]; return 0; }
0man regex.h
- Sau đó nối ký tự thành một đầu ra tạm thời có cùng kích thước.
1man regex.h
- Và để tiếp tục thêm chúng ta đổi thành
2 chỉ sau khi xác định được ký tự thẻ đóngman regex.h
3man regex.h
- Tôi đã chèn một vòng lặp
Mã cuối cùng là
var doc = new HtmlDocument[];
doc.LoadHtml[d];
2đầu ra có thể xảy ra.
2internal static string RmvTags[string d] { if[string.IsNullOrEmpty[d]] return string.Empty; var doc = new HtmlDocument[]; doc.LoadHtml[d]; var accTags = new String[] { "strong", "em", "u"}; var n = new Queue[doc.DocumentNode.SelectNodes["./*|./text[]"]]; while[n.Count > 0] { var no = nodes.Dequeue[]; var pNo = no.ParentNode; if[!accTags.Contains[no.Name] && no.Name != "#text"] { var cNo = no.SelectNodes["./*|./text[]"]; if [cNo != null] { foreach [var c in cNo] { n.Enqueue[c]; pNo.InsertBefore[c, no]; } } pNo.RemoveChild[no]; } } return doc.DocumentNode.InnerHtml; }
Điều đúng đắn là phân bổ không gian trên heap, bởi vì một chuỗi chứa tài liệu có thể rất lớn. Nhưng đối với các mục đích giáo khoa và để hiểu logic, nó có kích thước tốt