GoLang htmlspecialchars_decode

is this article helpful? yes | no
GoLang replacement for PHP's htmlspecialchars_decode [Golang Play | edit | history]
package main

import (
	"fmt"
	"golang.org/x/net/html"
)

func main() {
	fmt.Println(htmlspecialchars_decode("Hello,&nbsp世界"))
}

func htmlspecialchars_decode(str string) string {
	return html.UnescapeString(str)
}

PHP htmlspecialchars_decode

PHP original manual for htmlspecialchars_decode [ show | php.net ]

htmlspecialchars_decode

(PHP 5 >= 5.1.0, PHP 7)

htmlspecialchars_decode Convert special HTML entities back to characters

Description

string htmlspecialchars_decode ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 ] )

This function is the opposite of htmlspecialchars(). It converts special HTML entities back to characters.

The converted entities are: &, " (when ENT_NOQUOTES is not set), ' (when ENT_QUOTES is set), < and >.

Parameters

string

The string to decode.

flags

A bitmask of one or more of the following flags, which specify how to handle quotes and which document type to use. The default is ENT_COMPAT | ENT_HTML401.

Available flags constants
Constant Name Description
ENT_COMPAT Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES Will convert both double and single quotes.
ENT_NOQUOTES Will leave both double and single quotes unconverted.
ENT_HTML401 Handle code as HTML 4.01.
ENT_XML1 Handle code as XML 1.
ENT_XHTML Handle code as XHTML.
ENT_HTML5 Handle code as HTML 5.

Return Values

Returns the decoded string.

Changelog

Version Description
5.4.0 The constants ENT_HTML401, ENT_XML1, ENT_XHTML and ENT_HTML5 were added.

Examples

Example #1 A htmlspecialchars_decode() example

<?php
$str 
"<p>this -&gt; &quot;</p>\n";

echo 
htmlspecialchars_decode($str);

// note that here the quotes aren't converted
echo htmlspecialchars_decode($strENT_NOQUOTES);
?>

The above example will output:

<p>this -> "</p>
<p>this -> &quot;</p>

See Also