SWIFT 3 - take out html tags from string taken from JSON web url
10,051
You can use this code for stripping html tags
From your previous question
guard let dic = post["summary"] as? [String: Any], let summary = dic["value"] as? String else {
return
}
let str = summary.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
print(str)
Edit
I have checked it and it is working
let summary = "<p>Latin text here</p>"
let str = summary.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil)
print(str)
Latin text here
Author by
rob
Updated on July 18, 2022Comments
-
rob almost 2 years
I was wondering how can HTML tags be stripped out of JSON from a web url. Do I have to use
NSString
of something similar.So I am looking to strip out the html tags that are in the summary value. I looked around abit and it says NSString can be used but I was not sure if that was something that could be implemented into Swift 3. Any Help would be appreciated.
My code:
import UIKit import Alamofire struct postinput { let mainImage : UIImage! let name : String! let author : String! let summary : String! } class TableViewController: UITableViewController { var postsinput = [postinput]() var mainURL = "https://www.example.com/api" typealias JSONstandard = [String : AnyObject] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. callAlamo(url: mainURL) } func callAlamo(url : String){ Alamofire.request(url).responseJSON(completionHandler: { response in self.parseData(JSONData: response.data!) }) } func parseData(JSONData : Data) { do { var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard // print(readableJSON) if let posts = readableJSON["posts"] as? [JSONstandard] { for post in posts { let title = post["title"] as! String let author = post["author"] as! String guard let dic = post["summary"] as? [String: Any], let summary = dic["value"] as? String else { return } print(author) if let imageUrl = post["image"] as? String { let mainImageURL = URL(string: imageUrl ) let mainImageData = NSData(contentsOf: mainImageURL!) let mainImage = UIImage(data: mainImageData as! Data) postsinput.append(postinput.init(mainImage: mainImage, name: title, author: author, summary: summary)) } } DispatchQueue.main.async { self.tableView.reloadData() } } } catch { print(error) } } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return postsinput.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") // cell?.textLabel?.text = titles[indexPath.row] let mainImageView = cell?.viewWithTag(2) as! UIImageView mainImageView.image = postsinput[indexPath.row].mainImage //(cell?.viewWithTag(2) as! UIImageView).image = postsinput[indexPath.row].mainImage let mainLabel = cell?.viewWithTag(1) as! UILabel mainLabel.text = postsinput[indexPath.row].name mainLabel.font = UIFont(name: "Helvetica", size:14) let autLabel = cell?.viewWithTag(3) as! UILabel autLabel.text = postsinput[indexPath.row].author autLabel.font = UIFont(name: "Helvetica", size:12) let sumLabel = cell?.viewWithTag(4) as! UILabel sumLabel.text = postsinput[indexPath.row].summary sumLabel.font = UIFont(name: "Helvetica", size:12) //(cell?.viewWithTag(3) as! UILabel).text = postsinput[indexPath.row].author return cell! } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
-
Frankie over 7 yearsCan you post an example of the response you're trying to parse?
-
rob over 7 yearsIts the JSON structure from (stackoverflow.com/questions/40526761/…)
-
Palpatim over 7 yearsPossible duplicate of Convert HTML to Plain Text in Swift
-
rob over 7 years@Palpatim, where would you put that extension code?
-
-
rob over 7 yearsNo error and the html tags are still there.... I have done 'guard let dic = post["summary"] as? [String: Any], let summary = dic["value"] as? String else { return let str = summary.replacingOccurrences(of: "<[^>]+>", with: "", options: .regularExpression, range: nil) print(str) }'
-
Rajat over 7 yearsyou don't need to put it in else condition
-
Rajat over 7 years@rob Can you post the string which you are getting in summary ?
-
rob over 7 years"summary": { "value": "<p>Latin text here</p>", "format": "filtered_html" }
-
Rajat over 7 years@rob i have checked and updated answer and it is working for me.When you are printing this string
print(str)
what are you getting in console ? -
rob over 7 yearsIn the console its printing fine but is in there.
-
Rajat over 7 years@rob What is that string ? give me that string so i will check it
-
rob over 7 yearsI have updated the code with the additional code for you to see.
-
Rajat over 7 years@rob you are saying it is printing correctly in console but not working on your cell ?
-
Rajat over 7 years@rob Then I have answers what is posted in your question.The string is correctly removing html tags, but you are not able to properly use it in you table view cell, for that you need to post another question, my job is done here.
-
Mansuu.... almost 7 yearsWhat if it is like " <span><em><img src='images/icon3.png' alt=></em><a href='#/userProfile/MQ==/MQ=='>John Snow</a> recognized <a href='#/userProfile/NQ==/MQ=='> Arya Stark</a> for Owns what's given</span>"