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
+9.9k Golang : Proper way to test CIDR membership of an IP 4 or 6 address example
+6.8k Android Studio : Rating bar example
+19.8k Golang : Get password from console input without echo or masked
+12.9k Golang : Convert IP version 6 address to integer or decimal number
+10.1k Golang : Find age or leap age from date of birth example
+12.1k Golang : Generate Code128 barcode
+20.5k Golang : Join arrays or slices example
+18k Golang : Execute shell command
+7.7k Golang : Gorilla web tool kit schema example
+9.3k JavaScript/JQuery : Detect or intercept enter key pressed example
+2.6k Java : Get FX sentiment from website example
+8k Golang : How to check if a string with spaces in between is numeric?