swift string转为html,Encode String to HTML String Swift 3

问题

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 &<> &angst; å π 新 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值