Substitute certain (by default, non-alphanumeric) characters with dashes and remove extra dashes at both ends to generate ID strings. This function is intended for generating IDs for HTML elements, so HTML tags in the input text will be removed first.
alnum_id(x, exclude = "[^[:alnum:]]+")
A character vector of IDs.
x = c("Hello world 123!", "a &b*^##c 456")
xfun::alnum_id(x)
#> [1] "hello-world-123" "a-b-c-456"
xfun::alnum_id(x, "[^[:alpha:]]+") # only keep alphabetical chars
#> [1] "hello-world" "a-b-c"
# when text contains HTML tags
xfun::alnum_id("<h1>Hello <strong>world</strong>!")
#> [1] "hello-world"