HTMLString allows you to add and remove HTML entities from a String.
🔏 Adding HTML Entities (Escape)
When a character is not supported into the specified encoding, the library will replace it with a decimal entity (supported by all browsers supporting HTML 4 and later).
For instance, the & character will be replaced by &.
You can choose between ASCII and Unicode escaping:
Use the addingASCIIEntities function to escape for ASCII-encoded content
Use the addingUnicodeEntities function to escape for Unicode-compatible content
💡 Pro Tip: When your content supports UTF-8 or UTF-16, use Unicode escaping as it is faster and produces a less bloated output.
Example
import HTMLString
let emoji = "My favorite emoji is 🙃"
let escapedEmoji = emoji.addingASCIIEntities() // "My favorite emoji is 🙃"
let noNeedToEscapeThatEmoji = emoji.addingUnicodeEntities() // "My favorite emoji is 🙃"
let snack = "Fish & Chips"
let escapedSnack = snack.addingASCIIEntities() // "Fish & Chips"
let weAlsoNeedToEscapeThisSnack = snack.addingUnicodeEntities() // "Fish & Chips"
📝 Removing HTML Entities (Unescape)
To remove all the HTML entities from a String, use the removingHTMLEntities function.
Example
import HTMLString
let escapedEmoji = "My favorite emoji is 🙃"
let emoji = escapedEmoji.removingHTMLEntities() // "My favorite emoji is 🙃"
let escapedSnack = "Fish & Chips"
let snack = escapedSnack.removingHTMLEntities() // "Fish & Chips"
Objective-C API
With Obj-C interoperability, you can import and use the HTMLString module from in Objective-C code.
The library introduces a set of Objective-C specific APIs as categories on the NSString type:
-[NSString stringByAddingUnicodeEntities]; : Replaces every character incompatible with HTML Unicode encoding by a decimal HTML entitiy.
-[NSString stringByAddingASCIIEntities]; : Replaces every character incompatible with HTML ASCII encoding by a decimal HTML entitiy.
-[NSString stringByRemovingHTMLEntities]; : Replaces every HTML entity with the matching Unicode character.
HTMLString
is a library written in Swift that allows your program to add and remove HTML entities in Strings.&
)Supported Platforms
This package requires Swift 5 and Xcode 12.
Installation
Swift Package Manager
Add this line to your
Package.swift
:CocoaPods
Add this line to your
Podfile
:Carthage
Add this line to your Cartfile:
Usage
HTMLString
allows you to add and remove HTML entities from a String.🔏 Adding HTML Entities (Escape)
When a character is not supported into the specified encoding, the library will replace it with a decimal entity (supported by all browsers supporting HTML 4 and later).
You can choose between ASCII and Unicode escaping:
addingASCIIEntities
function to escape for ASCII-encoded contentaddingUnicodeEntities
function to escape for Unicode-compatible contentExample
📝 Removing HTML Entities (Unescape)
To remove all the HTML entities from a String, use the
removingHTMLEntities
function.Example
Objective-C API
With Obj-C interoperability, you can import and use the
HTMLString
module from in Objective-C code.The library introduces a set of Objective-C specific APIs as categories on the
NSString
type:-[NSString stringByAddingUnicodeEntities];
: Replaces every character incompatible with HTML Unicode encoding by a decimal HTML entitiy.-[NSString stringByAddingASCIIEntities];
: Replaces every character incompatible with HTML ASCII encoding by a decimal HTML entitiy.-[NSString stringByRemovingHTMLEntities];
: Replaces every HTML entity with the matching Unicode character.Escaping Examples
Unescaping Examples
Author
License
HTMLString is available under the MIT license. See the LICENSE file for more info.