PHP SEO Slug URL Generator

In this video we’re going to create a
slug generator or a “SEO URL friendly” generator basically
allows us to take a string like this and go ahead and
convert it to something like this, and we’ll be looking at regular
expressions in PHP and this will basically allow us to just
quickly and efficiently remove what we don’t need and add what we need to make it look like this.
Let’s go ahead and jump straight into how the code looks. Okay, so over to our text editor we’re now going to go ahead and start writing the PHP that we need to build up this functionality. You can
see the I’m starting out with a doctype here and the reason for this is that I have a charset on my page. Now what we’re
going to be doing is we’re also going to be including characters like these in now SEO URL generation and typically
this wouldn’t work so we need to go ahead and at unicode support when we create our regular
expressions so that’s why I’ve created this doctype. We’re going to go ahead and create the function up here and call the function down here in
the body and because we specifically define the charset on our page this will allow characters to look as they do.
Now we’ll go ahead and take a look towards the end of the video at what
happens if you don’t declare charset and the problems you’ll
probably see in browsers if you don’t. So the first thing we really want to do here is just at the top here go ahead and declare a function and this
can be done prefereably in an external function file or even better as part of a class, maybe have a
generic string handling class your application
but we’ll go ahead and create a function here just for simplicity so we can pass a
string in and get a string out. Let’s go ahead and
call this function ‘createSlug’ and we need to go ahead and we’ll just define an argument here and
call it ‘slug’ so there are a couple of things that we need to do here so we’ll go ahead and add some comments just so we know what we need to do. The first
thing that we need to do is basically removed anything that isn’t letters, numbers, spaces or hyphens from the original string so we need to remove letters, sorry anything that isn’t. So we need anything but letters removed, anything
but numbers removed anything but spaces removed and anything
but hypens removed and the reason we do this is
because we want to remove, well, we also want to afterwards remove things like spaces and duplicate hyphens so we’re going to do two regular
expressions, the first one to do this and then the second one just clear
everything up. So then we want to go ahead and remove spaces and duplicate hyphens because we don’t
want to go ahead and include two or three hyphens within a sentence
if hypens have been included. What we then want to do is trim the left and right, removing any leftover
hyphens So for example if you have a string
like just go ahead and write it down here.
A string like this this isn’t acceptable so you want to go
ahead and just trim so we’ll just say ‘test’ here and we want to go ahead and trim the left and right hypens so that’s what we want to do there and that
is pretty much it, then we just want to return the string. Let’s go ahead and focus on this we’ll leave the comments just up
here and we’ll go ahead and write all the code down here Now the first thing we want to do is redefine the slug variable and this is going to basically be the
first line here “removed anything that isn’t letters, number, spaces or hyphens”
So in PHP we use preg_replace or “Perl Regular Expression” replace and we go ahead and pass our regular
expression in here we then choose what we want to replace this with and in this case we are removing so all we need to do is
replace with an empty string so then what we want to go ahead and pass in the string that we want to apply this to and that’s obviously the slug. Now in a
minute we’re gonna go ahead and look at this and applying a function here to set the lower case value of this. We’ll go ahead and concentrate on
the regular expression first because that’s probably the most difficult part. So let’s go
ahead and give our regular expression and name, a descriptive name I’m gonna call this lettersNumbersSpacesHypens this is basically going to remove everything that isn’t a letter, number, space or hypen.
Let’s go ahead and put our start and end to our regular expression.
I’ve chosen forward slash And we’re going to add unicode support to this, so this called a modifier and the reason we need to provide support for Unicode
characters is because we’re using special characters with accents or anything like that. So
within here what do we want to do? Well we want to group something in here, and this
means match any so anything we put in here is going
match anything but we want to match everything but
these so we go ahead and use this here so now what we go ahead and do is provide the characters or the representation of what we want to keep. In this case it’s a hyphen so we
can’t just add hyphen here unfortunately have to do a backslash hyphen. That just escapes this as something we want to use literally, not
as some kind of rule or pattern. So then we also
want to remove spaces, that’s just sorry, anything but spaces… that’s just
s, again this is just the way the modifier looks. Then want to use pN for numbers this is because we’re using unicode support, so pN will basically represent numbers and pL you probably guessed represents letters. We also want to add the + modifier on here because we want to exercise this across the entire string So now that we’ve done that
let’s go ahead and look at testing this out. I’m gonna go ahead and
copy and paste this regular expression down here, and remember we’re replacing with nothing Let’s just go ahead and call this function
and see where where we’re at at the moment so I’m going to go ahead and echo createSlug then go ahead and pass a string in. So I’m going to type “This is an article title” That’ll do for now I suppose. Let’s go ahead and refresh, and oops we didn’t return anything so let’s go ahead and return slug. So we’ve returned that from the function and we’re echoing out the result of the function here
and it does absolutely nothing so the reason it’s not doing anything at the moment is because we’re not adding any hypens in anywhere and we’re also not setting this to lower
case and we do need this to be a lower case string so what we want to do now is test this out by adding a few hyphens, some extra spaces here and then go ahead and add maybe some funny
characters So let’s go ahead and look at the result of this. Perfect, what it’s done is removed the special
characters on the end here like all the rubbish I just typed in. We’ve got this duplicate hypen issue but that’s fine we can
go ahead and deal with that later. Let’s go ahead and do this so at the moment we’re going to remove
lots of spaces, this basically means find lots of spaces within the string as we’ve already mentioned so this is lots of spaces and that’s
why this has been removed So that’s been replaced with nothing but now we
want to go ahead and do is say slug preg_replace again so a perl regular expression replace again and we
wanna go ahead and add a regular expression and this time we want to replace lots of
things with hyphens and that’s again applied to the slug. So let’s take a look at what we need to do here we need to replace any amount of hyphens with one hyphen because we don’t want duplicate hypens we also want to have any amount of space replaced with a hypen as well so let’s go ahead and do that now.
So the regular expression is going to look like this and we’ll go ahead and give this a name
something like spacesAndDuplicateHypens Ok, so the regular expression obviously starts off
with two characters like this so I’ve chosen forward slash and we
want to go ahead and group these so any of these. I’m going to again say – and s and we want to match any number of these
so any number of hyphens in that case is remove these and any number of spaces so
that will replace all spaces with a hyphen and all hyphens with the single hyphen that’s exactly what we want because we want to remove those duplicates, so if we replace that in there Let’s go ahead echo the slug out now
and there we go So we’re halfway there but the only problem is that we’ve got a hypen on the end of the string which we’ll go ahead and
remove with the trim function and we also need to convert to lower case
because at the moment I can write anything I want in upper case
and it will remain in that case, so what we do he is we change
it at this point here So I’m going to go ahead and use the mb_strtolower function. What this allows me to do is
provide particular charset so let’s start to bring in some special characters here and we’ll go ahead and look at how this works What’s happened here is the regular expression has silently If I remove that you can see everything looks fine here so when we go ahead and use something like strtolower this works fine. But we don’t want to use strtolower it’s not recommended that we do use this particularly when we’re dealing with different
characters so we want to use mb_strtolower and then go ahead and
define the character encoding and in this case it’s utf8 and this is going to work now, you’ll see that works. I’m refreshing and we get the same result and everything is now lower case and we
catered for the exact string length the exact character encoding. So now what we want to do is finally just go ahead and trim off the hyphen. You might be familiar with
the PHP trim function but what you may not know is that you
can actually define what you want to trim from the left and the right hand
side If it was just a space you could define it
like this but we’re going to go ahead and choose hyphen and we want to remove them from the left and the right in this case we’ve got one on the right hand side so when we refresh it disappears So what we’ve done with a few fairly complex regular expressions but nothing too difficult. If you’re new to regular
expressions these might look quite complicated we’ve gone ahead and taken a string like this, a horrible looking string like this and we’ve
converted it something that could safely be used in a URL

16 thoughts on “PHP SEO Slug URL Generator

  1. as I see it, you didn't really converted the string to safe url. the character "a" still is a special character. is an "a" with accent.

  2. Wow.. the YouTube auto captions work surprisingly well. Almost as if Alex just manually typed all of them in.

  3. Hi Codecourse;

    Thank you very much for this example.

    Do you have php and htaccess seo friendly url redirect the expression?

  4. Great recommendations. They remind me of the Semalt marketing approach. I believe that there can be as many recommendations as there are SEO experts. So, as for me, the right decision would be to choose your company that will handle your digital marketing and SEO issues on a regular basis. That's all about (email address checker tools)

  5. Can someone please tell me how to have completey dynamic URL's such as directly instead of ?

Leave a Reply

Your email address will not be published. Required fields are marked *