Golang : Format strings to SEO friendly URL example
Problem:
You want to take a string and format it to SEO friendly URL. SEO friendly means no uppercase, underscore and characters deemed not suitable for search engine crawlers. How to do that?
NOTES: This example can be used in sanitazing and cleaning up input strings as well.
Solution:
Ported this PHP function to Golang
function SEOfriendlyURL($string)
{
$string = strtolower($string);
$string = str_replace(" ", "-", $string);
$string = str_replace("/", "-", $string);
$string = preg_replace('/\s+/', '-', $string);
$string = preg_replace("`\[.*\]`U", "", $string);
$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i', '-', $string);
$string = preg_replace("/[^\x9\xA\xD\x20-\x7F]/", "", $string);
$string = htmlentities($string, ENT_COMPAT, 'utf-8');
$string = preg_replace("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i", "\\1", $string);
$string = preg_replace(array("`[^a-z0-9]`i", "`[-]+`"), "-", $string);
return strtolower(trim($string, '-'));
}
and added some improvements to normalize unicode strings and remove all diacritical/accents marks.
Here you go!
package main
import (
"fmt"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
"regexp"
"strings"
"unicode"
)
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
func SEOURL(s string) string {
seoStr := strings.ToLower(s)
//seoStr = strings.Replace(seoStr, "/", "-", -1)
//regE := regexp.MustCompile("/s+/")
//seoStrByte := regE.ReplaceAll([]byte(seoStr), []byte("-"))
//seoStr = string(seoStrByte) // convert []byte to string
// convert all spaces to dash
regE := regexp.MustCompile("[[:space:]]")
seoStrByte := regE.ReplaceAll([]byte(seoStr), []byte("-"))
seoStr = string(seoStrByte) // convert []byte to string
// remove all blanks such as tab
regE = regexp.MustCompile("[[:blank:]]")
seoStrByte = regE.ReplaceAll([]byte(seoStr), []byte(""))
seoStr = string(seoStrByte) // convert []byte to string
// remove all punctuations with the exception of dash
//regE = regexp.MustCompile("[[:punct:]]")
regE = regexp.MustCompile("[!/:-@[-`{-~]")
seoStrByte = regE.ReplaceAll([]byte(seoStr), []byte(""))
seoStr = string(seoStrByte) // convert []byte to string
// \x9\xA\xD will cause non-hex character in escape sequence error
// regE = regexp.MustCompile("/[^\x9\xA\xD\x20-\x7F]/")
//regE = regexp.MustCompile("[[:xdigit:]]") -- will remove some alphabet. Bug?
regE = regexp.MustCompile("/[^\x20-\x7F]/")
seoStrByte = regE.ReplaceAll([]byte(seoStr), []byte(""))
seoStr = string(seoStrByte) // convert []byte to string
regE = regexp.MustCompile("`&(amp;)?#?[a-z0-9]+;`i")
seoStrByte = regE.ReplaceAll([]byte(seoStr), []byte("-"))
seoStr = string(seoStrByte) // convert []byte to string
regE = regexp.MustCompile("`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i")
seoStrByte = regE.ReplaceAll([]byte(seoStr), []byte("\\1"))
seoStr = string(seoStrByte) // convert []byte to string
regE = regexp.MustCompile("`[^a-z0-9]`i")
seoStrByte = regE.ReplaceAll([]byte(seoStr), []byte("-"))
seoStr = string(seoStrByte) // convert []byte to string
regE = regexp.MustCompile("`[-]+`")
seoStrByte = regE.ReplaceAll([]byte(seoStr), []byte("-"))
seoStr = string(seoStrByte) // convert []byte to string
// normalize unicode strings and remove all diacritical/accents marks
// see https://www.socketloop.com/tutorials/golang-normalize-unicode-strings-for-comparison-purpose
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
seoStr, _, _ = transform.String(t, seoStr)
return strings.TrimSpace(seoStr)
}
func main() {
NonSEOString := "@<ElNi\u00f1o coming? > #% sooner this year!"
fmt.Println("BEFORE : ", NonSEOString)
SEOedString := SEOURL(NonSEOString)
fmt.Println("AFTER : ", SEOedString)
}
Output:
BEFORE : @<ElNiño coming? > #% sooner this year!
AFTER : elnino-coming--#%-sooner-this-year
NOTES: This example is not perfect as regular expression is not exactly my forte. Also, the conversion from byte to string can be optimized instead of converting in and out. Will leave it as an exercise for you. ;-)
References:
https://golang.org/pkg/regexp/#Regexp.ReplaceAll
https://golang.org/pkg/regexp/syntax/#pkg-overview
https://www.socketloop.com/tutorials/golang-normalize-unicode-strings-for-comparison-purpose
https://www.socketloop.com/tutorials/trim-white-spaces-string-golang
See also : Golang : Normalize unicode strings for comparison purpose
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+4.8k Unix/Linux : How to pipe/save output of a command to file?
+3.8k Golang : Switch Redis database redis.NewClient
+17.1k Golang : Capture stdout of a child process and act according to the result
+37.7k Golang : Comparing date or timestamp
+6.3k Golang : Calculate US Dollar Index (DXY)
+16.4k CodeIgniter/PHP : Create directory if does not exist example
+22.7k Generate checksum for a file in Go
+7.3k Golang : File system scanning
+9.4k Golang : Find the length of big.Int variable example
+12.5k Golang : HTTP response JSON encoded data
+10.2k Golang : Bcrypting password
+6.2k Golang : How to write backslash in string?