how-to: slugify

Nov 13, 2009 at 6:07 PM
Edited Nov 13, 2009 at 6:21 PM

Hello,

I noticed that when you insert posts to be_posts, the slug field stays NULL. If you go into the front end and specifically click "extract from title" option or manually create a post, that SLUG column does store a value.  NULL value slug doesn't seem to impact anything, the normal slug is still generated fine for posts with NULL slug column.

I have another requirement though where I need to create an index of the posts on in blog.  I did write a web crawlerm for this, but lost the code and don't want to re-write it.  Is there an easy way to populate the slug column for existing posts? 

If not, then I will just try to create some sproc in SQL that mimics the transforms you do in the .net source code to slugify the string....

 

Thanks!!

Coordinator
Nov 17, 2009 at 7:24 AM

Here's a SQL UDF that mimics the Utils.RemoveIllegalCharacters() function.  If you pass the Title to this UDF, it should return a value that can be used as the slug.

CREATE FUNCTION [dbo].[RemoveIllegalCharacters]
(
	@input nvarchar(4000)
)
RETURNS nvarchar(4000)
AS
BEGIN
	DECLARE @output nvarchar(4000);
	
	SET @output = 
		REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
		REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
		REPLACE(REPLACE(REPLACE(@input, ':', ''), '/', ''), '?', ''),
		'#', ''), '[', ''), ']', ''), '@', ''), '*', ''), '.', ''),
		',', ''), '"', ''), '&', ''), '''', ''), ' ', '-'), '--', '-');
	
	RETURN @output;
END
GO

Nov 17, 2009 at 3:48 PM

Thanks for this!  I also wrote this on my own, but missed some stuff so thanks!

Also, I actually did not end up using a slugged string... I realized I could put anything in that slug field so I just dynamically created my own titles based on some content from each post's row... i will replace my function with yours to ensure web safeness

 

THANK YOU SO MUCH FOR ALL THE WORK ON BE.NET, IT IS TRULY AWESOME!!!