আমি কোনও স্ট্রিং থেকে এইচটিএমএল ট্যাগগুলি কীভাবে সরিয়ে ফেলব যাতে আমি পরিষ্কার পাঠ্য আউটপুট করতে পারি?
let str = string.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)
print(str)
আমি কোনও স্ট্রিং থেকে এইচটিএমএল ট্যাগগুলি কীভাবে সরিয়ে ফেলব যাতে আমি পরিষ্কার পাঠ্য আউটপুট করতে পারি?
let str = string.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)
print(str)
উত্তর:
হুম, আমি আপনার ফাংশনটি চেষ্টা করেছিলাম এবং এটি একটি ছোট উদাহরণে কাজ করেছে:
var string = "<!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html>"
let str = string.stringByReplacingOccurrencesOfString("<[^>]+>", withString: "", options: .RegularExpressionSearch, range: nil)
print(str)
//output " My First Heading My first paragraph. "
আপনি একটি সমস্যার উদাহরণ দিতে পারেন?
সুইফট 4 এবং 5 সংস্করণ:
var string = "<!DOCTYPE html> <html> <body> <h1>My First Heading</h1> <p>My first paragraph.</p> </body> </html>"
let str = string.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
<p foo=">now what?">Paragraph</p>
string.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)
যেহেতু এইচটিএমএল কোনও নিয়মিত ভাষা নয় (এইচটিএমএল একটি প্রসঙ্গমুক্ত ভাষা), আপনি নিয়মিত এক্সপ্রেশন ব্যবহার করতে পারবেন না। দেখুন: এইচটিএমএল পার্স করতে নিয়মিত এক্সপ্রেশন ব্যবহার করে: কেন নয়?
আমি এর পরিবর্তে এনএসএট্রিবিউটেড স্ট্রিং ব্যবহার বিবেচনা করব।
let htmlString = "LCD Soundsystem was the musical project of producer <a href='http://www.last.fm/music/James+Murphy' class='bbcode_artist'>James Murphy</a>, co-founder of <a href='http://www.last.fm/tag/dance-punk' class='bbcode_tag' rel='tag'>dance-punk</a> label <a href='http://www.last.fm/label/DFA' class='bbcode_label'>DFA</a> Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of <a href='http://www.last.fm/tag/alternative%20dance' class='bbcode_tag' rel='tag'>alternative dance</a> and <a href='http://www.last.fm/tag/post%20punk' class='bbcode_tag' rel='tag'>post punk</a>, along with elements of <a href='http://www.last.fm/tag/disco' class='bbcode_tag' rel='tag'>disco</a> and other styles. <br />"
let htmlStringData = htmlString.dataUsingEncoding(NSUTF8StringEncoding)!
let options: [String: AnyObject] = [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding]
let attributedHTMLString = try! NSAttributedString(data: htmlStringData, options: options, documentAttributes: nil)
let string = attributedHTMLString.string
বা মন্তব্যগুলিতে ইরশাদ মোহাম্মদ যেমনটি করতেন:
let attributed = try NSAttributedString(data: htmlString.data(using: .unicode)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
print(attributed.string)
let attributed = try NSAttributedString(data: htmlString.data(using: .unicode)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil) print(attributed.string)বেশিরভাগ লোক উত্তরগুলি পছন্দ করতে পছন্দ করে যা ছোট এবং সহজেই বোঝা যায়।
মোহাম্মদ সমাধান তবে সুইফট 4-এ স্ট্রিং এক্সটেনশন হিসাবে।
extension String {
func stripOutHtml() -> String? {
do {
guard let data = self.data(using: .unicode) else {
return nil
}
let attributed = try NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil)
return attributed.string
} catch {
return nil
}
}
}
আমি নির্দিষ্ট এইচটিএমএল উপাদানগুলি সরাতে নিম্নলিখিত এক্সটেনশনটি ব্যবহার করছি:
extension String {
func deleteHTMLTag(tag:String) -> String {
return self.stringByReplacingOccurrencesOfString("(?i)</?\(tag)\\b[^<]*>", withString: "", options: .RegularExpressionSearch, range: nil)
}
func deleteHTMLTags(tags:[String]) -> String {
var mutableString = self
for tag in tags {
mutableString = mutableString.deleteHTMLTag(tag)
}
return mutableString
}
}
এটি কেবল <a>স্ট্রিং থেকে ট্যাগগুলি মুছে ফেলা সম্ভব করে তোলে , যেমন:
let string = "my html <a href="">link text</a>"
let withoutHTMLString = string.deleteHTMLTag("a") // Will be "my html link text"
extension String{
var htmlStripped : String{
return self.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
}
}
শুভ কোডিং
দ্রুত 4:
extension String {
func deleteHTMLTag(tag:String) -> String {
return self.replacingOccurrences(of: "(?i)</?\(tag)\\b[^<]*>", with: "", options: .regularExpression, range: nil)
}
func deleteHTMLTags(tags:[String]) -> String {
var mutableString = self
for tag in tags {
mutableString = mutableString.deleteHTMLTag(tag: tag)
}
return mutableString
}
}
সুইফট 4 এর জন্য আপডেট হয়েছে:
guard let htmlStringData = htmlString.data(using: .unicode) else { fatalError() }
let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [
.documentType: NSAttributedString.DocumentType.html
.characterEncoding: String.Encoding.unicode.rawValue
]
let attributedHTMLString = try! NSAttributedString(data: htmlStringData, options: options, documentAttributes: nil)
let string = attributedHTMLString.string
আমি এনএসএট্রিবিউটেড স্ট্রিং এইচটিএমএল রূপান্তর ব্যবহার করার চেয়ে নিয়মিত ভাব প্রকাশ করতে পছন্দ করি, এটি পরামর্শ দিন যে এটি বেশ সময় সাশ্রয়ী এবং মূল থ্রেডে চালানোও দরকার। এখানে আরও তথ্য: https://developer.apple.com/docamentation/foundation/nsattributesstring/1524613-initwithdata
আমার জন্য এটি কৌশলটি তৈরি করে, প্রথমে আমি কোনও সিএসএস ইনলাইন স্টাইলিং এবং পরে সমস্ত এইচটিএমএল ট্যাগ মুছে ফেলি। সম্ভবত এনএসএট্রিবিউটেড স্ট্রিং বিকল্প হিসাবে শক্ত নয়, তবে আমার ক্ষেত্রে দ্রুততর way
extension String {
func withoutHtmlTags() -> String {
let str = self.replacingOccurrences(of: "<style>[^>]+</style>", with: "", options: .regularExpression, range: nil)
return str.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
}
}