问题
How can I encode a string in swift to remove all special characters and replace it with its matching html number.
Lets say I have the following String:
var mystring = "This is my String & That's it."
and then replace the special characters with its html number
& = &
' = '
> = >
But I want to do this for all Special Characters not just the ones listed in the string above. How would this be done?
回答1:
extension String {
func makeHTMLfriendly() -> String {
var finalString = ""
for char in self {
for scalar in String(char).unicodeScalars {
finalString.append("\(scalar.value)")
}
}
return finalString
}
}
Usage:
newString = oldString.makeHTMLfriendly()
This appears to work in general (although I don't know for sure that unicode scalars always match HTML numbers).
Note that it converts everything, even things like alphanumeric characters that don't really need to be converted. It probably wouldn't be too difficult to edit it to not convert some things.
回答2:
Check the all special characters in html:
http://www.ascii.cl/htmlcodes.htm
And you create a Util for parsing the characters:
like this:
import UIKit
class Util: NSObject {
func parseSpecialStrToHtmlStr(oriStr: String) -> String {
var returnStr: String = oriStr
returnStr = returnStr.replacingOccurrences(of: "&", with: "&")
returnStr = returnStr.replacingOccurrences(of: "'", with: "'")
returnStr = returnStr.replacingOccurrences(of: ">", with: ">")
...
return returnStr
}
}
Do it yourself, create yourself's functional device.
Edit
If you think its a huge work, you check this: https://github.com/adela-chang/StringExtensionHTML
回答3:
Try SwiftSoup
func testEscape()throws {
let text = "Hello &<> Å å π 新 there ¾ © »"
let escapedAscii = Entities.escape(text, OutputSettings().encoder(String.Encoding.ascii).escapeMode(Entities.EscapeMode.base))
let escapedAsciiFull = Entities.escape(text, OutputSettings().charset(String.Encoding.ascii).escapeMode(Entities.EscapeMode.extended))
let escapedAsciiXhtml = Entities.escape(text, OutputSettings().charset(String.Encoding.ascii).escapeMode(Entities.EscapeMode.xhtml))
let escapedUtfFull = Entities.escape(text, OutputSettings().charset(String.Encoding.utf8).escapeMode(Entities.EscapeMode.extended))
let escapedUtfMin = Entities.escape(text, OutputSettings().charset(String.Encoding.utf8).escapeMode(Entities.EscapeMode.xhtml))
XCTAssertEqual("Hello &<> Å å π 新 there ¾ © »", escapedAscii)
XCTAssertEqual("Hello &<> Å å π 新 there ¾ © »", escapedAsciiFull)
XCTAssertEqual("Hello &<> Å å π 新 there ¾ © »", escapedAsciiXhtml)
XCTAssertEqual("Hello &<> Å å π 新 there ¾ © »", escapedUtfFull)
XCTAssertEqual("Hello &<> Å å π 新 there ¾ © »", escapedUtfMin)
// odd that it's defined as aring in base but angst in full
// round trip
XCTAssertEqual(text, try Entities.unescape(escapedAscii))
XCTAssertEqual(text, try Entities.unescape(escapedAsciiFull))
XCTAssertEqual(text, try Entities.unescape(escapedAsciiXhtml))
XCTAssertEqual(text, try Entities.unescape(escapedUtfFull))
XCTAssertEqual(text, try Entities.unescape(escapedUtfMin))
}
回答4:
This is the easiest way:
let encodedValue = yourValue.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
来源:https://stackoverflow.com/questions/42288963/encode-string-to-html-string-swift-3