SLUGIFY

=SLUGIFY(reference, ndx)

reference
Required. The cell reference (or string value) that contains the string to be converted to a URL slug.
ndx
Required. Starting character index. Should be entered as 1 to convert the entire string.

SLUGIFY function converts the given string to a URL slug.

smozgur

BatCoder
Joined
Feb 28, 2002
Messages
2,659
SLUGIFY function converts the given string to a URL slug.

We often use the document title as the page name to create a web page, which is called "URL slug". The document title often has invalid characters that cannot be used in the URL. Therefore, we generate a clean version of the title by replacing the invalid characters with a dash character. This function takes a given string and converts it to a URL slug by allowing only alphanumeric characters and dash.

This is a recursive function, and the second parameter is used as the loop counter.

Excel Formula:
=LAMBDA(reference, ndx,
  IF(ndx > LEN(reference),
    SUBSTITUTE(reference, "--", "-"),
    SLUGIFY(
      LET(
        character, LOWER(MID(reference, ndx, 1)),
        charcode, CODE(character),
        LEFT(reference, ndx - 1) & IF(OR(AND(charcode > 96, charcode < 123), AND(charcode > 47, charcode < 58)), character, "-") & RIGHT(reference, LEN(reference) - ndx)
      ),
      ndx + 1
    )
  )
)
SLUGIFY(reference, ndx)
AB
1Article TitleURL Slug
2Start Coding with Doctrine©2 ORMstart-coding-with-doctrine-2-orm
3Searching in Online Book Catalogsearching-in-online-book-catalog
4Pagination with Zend\Paginatorpagination-with-zend-paginator
5Model Mapper Connected to Database Table - Zend\Dbmodel-mapper-connected-to-database-table--zend-db
6Model●View●Controller (MVC)model-view-controller-mvc-
Sheet1
Cell Formulas
RangeFormula
B2:B6B2=SLUGIFY(A2,1)
 
Upvote 0
Now that's cool.
Isn't it? I really love Lambda function!

The second parameter is actually only a counter, but Lambda doesn't support optional parameters which is actually not a big deal to implement. So it looks like we have to add it to the function to create a loop like this.
There is a suggestion in Excel.Uservoice that I also commented, but who knows when they will consider implementing that.
 
I suppose you could use a wrapper function SLUGIFY(A1) which called the real Lambda(reference, ndx) function
 
I suppose you could use a wrapper function SLUGIFY(A1) which called the real Lambda(reference, ndx) function
You mean the following, right?

Excel Formula:
=LAMBDA(reference, SLUGIFY(reference, 1))(A1)

I didn't quite like that it requires an additional function definition, but it is actually a great solution until Microsoft allows optional parameters.
 
I originally wanted to create a recursive function to demonstrate a simple "for-next" implementation. However, it is also possible to create a non-recursive version.
A slightly modified version that uses validchar variable for readability.

Excel Formula:
=LAMBDA(reference,
  TEXTJOIN("",
    TRUE,
    LET(
      character, LOWER(MID(reference,SEQUENCE(LEN(reference),,1),1)),
      charcode, CODE(character),
      validchar, (charcode > 96) * (charcode < 123) + (charcode > 47) * ( charcode < 58),
      IF(validchar, character, "-")
    ))
)(A2)

Note: We can't use AND/OR functions here since these functions will aggregate the results and return a single value instead of an array when they are used with an array function. So we should simply use boolean math, */+ equivalent for AND and OR respectively.
 
Posted a new Lambda function demonstrating how to create and use subroutines in a Lambda function.

In SLUGIFY.PLUS function, I am redirecting the process to a subroutine to clean the multiple adjacent dashes in the final slug by using an additional recursion instead of using the SUBSTITUTE function that actually cannot clean all possible adjacent dashes.

 
Interesting.

You can Simplify LEFT(phrase, ndx-1) & IF(OR(AND(charcode>97,charcode<123), AND(charcode>47,charcode<58)), char, "-") & RIGHT(phrase, LEN(phrase)-ndx)) to Simply IF((ABS(charcode-109.5)<=12.5)+(ABS(charcode-52.5)<=4.5), char, REPLACE(phrase, ndx, 1, "-"))

Thanks for the non-recursive LAMBDA too!
 
Great recursive function smozgur !! I think it was the very first one on this forum. ✌️
As much as I love recursive functions, nowadays, with the amazing new lambda helper functions, we can replace their functionality in even more efficient ways.
To be more exact, every recursive design based on a determined nr. of iterations beforehand (like nr. of chars in a string) can be replaced with a lambda helper function.
The only recursive functions that will survive are the ones on which the nr. of iterations is undermined and only when the exit condition is met, will deliver the result.
Even those can be tricked with helper functions, but they will never be as "elegant" as the recursive ones.

Back to the chase, here we need to identify digits and lower letters, which can be done with their character code values and comparison operations.
What if we need to also keep the upper letters, this implies even more comparison operations, no matter how much they can be simplified.
What if we need to keep any letter from whatever alphabet, we cannot rely on char codes anymore.
There is a simpler universal way to identify a letter from any alphabet, lower or upper case, which is:
A letter is a character that has different values between its lower- and upper-case values.
On other words, for a letter "x" the formula =EXACT(LOWER(x),UPPER(x)) will return FALSE
If returns TRUE could be a digit or a symbol. Digits are numbers. So, a symbol is a non-number and a non-letter.

Using this approach and replacing recursive functionality with MAP, here is an alternative function for entire column vector input data.
Also, the first variable after LET, "g", is the dash char "-" ; LET(g,"-", ......
If we need underscore char "_" we can change it with ease.
The function can handle any distribution of dash chars or any other distribution of symbols, located anywhere trough the strings, beginning, end, middle.

AMSLG(a) Array MAP SLuGify
a: column vector (1D vertical array)
Excel Formula:
=LAMBDA(a,
    LET(
        g, "-",
        m, MID(LOWER(a), SEQUENCE(, MAX(LEN(a))), 1),
        p, MAP(m, LAMBDA(x, OR(ISNUMBER(--x), NOT(EXACT(LOWER(x), UPPER(x)))))),
        i, IF(p, m, " "),
        b, BYROW(i, LAMBDA(x, CONCAT(x))),
        SUBSTITUTE(TRIM(b), " ", g)
    )
)
SLG.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJ
1AMSLG Concept
21. extracting all chars of LOWER(a)
3a=MID(LOWER(B4:B5),SEQUENCE(,MAX(LEN(B4:B5))),1)
4--- /Aa2 --- (fgX \23-YZ )) ---- /aa2 --- (fgx \23-yz )) -
5Aa2/@ \34|--- --6xY7 ))aa2/@ \34|--- --6xy7 ))
6
72. identifing letters or digits
8=--MAP(D4#,LAMBDA(x,OR(ISNUMBER(--x),NOT(EXACT(LOWER(x),UPPER(x))))))
90000011100000000111001101100000
101110000110000000111100000000000
11
123. keeping letters and digits, the rest replaced with spaces " "
13=IF(D9#,D4#," ")
14 aa2 fgx 23 yz
15aa2 34 6xy7
16
174. appending byrow
18=BYROW(D14#,LAMBDA(x,CONCAT(x)))
19 aa2 fgx 23 yz
20aa2 34 6xy7
21
225. trimming and replacing spaces with dash char "-"
23=SUBSTITUTE(TRIM(B19#)," ","-")
24aa2-fgx-23-yz
25aa2-34-6xy7
26
AMSLG
Cell Formulas
RangeFormula
D3,B23,B18,D13,D8D3=FORMULATEXT(D4)
D4:AH5D4=MID(LOWER(B4:B5),SEQUENCE(,MAX(LEN(B4:B5))),1)
D9:AH10D9=--MAP(D4#,LAMBDA(x,OR(ISNUMBER(--x),NOT(EXACT(LOWER(x),UPPER(x))))))
D14:AH15D14=IF(D9#,D4#," ")
B19:B20B19=BYROW(D14#,LAMBDA(x,CONCAT(x)))
B24:B25B24=SUBSTITUTE(TRIM(B19#)," ","-")
Dynamic array formulas.
 

Forum statistics

Threads
1,223,884
Messages
6,175,174
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top