user622378
user622378

Reputation: 2336

Search Engine Friendly URL (SEF/SEO)

I want friendly URL names converted to:

Tom's Fish & Chips
Slug: tom-fish-and-Chips

1-2-3 Pizza
slug: 1-2-3-Pizza

Here a function:

<?php

function urlTitle($title) {
    $title = preg_replace("/(.*?)([A-Za-z0-9\s]*)(.*?)/", "$2", $title);
    $title = str_replace(' ', '-', $title);
    $title = strtolower($title);
    return $title;
}

echo urlTitle("Tom's Fish & Chips");
echo "<br />";
echo urlTitle("1-2-3 Pizza");

?>

The behaviour of the above function is almost what I want, since I get:

toms-fish--chips
123-pizza

How can I fix it?

Upvotes: 1

Views: 673

Answers (4)

designosis
designosis

Reputation: 5263

Webarto's version is perfect. I thought maybe this version could be useful to someone because it takes different things into consideration, such as transliteration, changing & to -and-, and entity decoding ... these things depend on how the data is stored in the db, etc. Note that this also assumes a UTF-8 environment :)

function seo( $str )
{
    $str = strip_tags($str); // tags out, just in case
    $str = html_entity_decode($str,ENT_QUOTES,'UTF-8'); // entities to text
    $str = strtolower($str); // lowercase
    $str = str_replace('&', ' and ', $str); // & to and
    $str = iconv('UTF-8','ASCII//TRANSLIT', $str); // transliterate
    $str = preg_replace('/[\s]/', '-', $str); // space to dash
    $str = preg_replace('/[^a-z0-9-]/', '', $str); // only alphanumeric and dash
    $str = preg_replace('#(-){2,}#', '$1', $str); // 2+ dashes to 1
    $str = trim($str, '-'); // no dashes at beginning or end
    return $str;
}

Upvotes: 0

vbence
vbence

Reputation: 20333

Put this in before the regex:

$title = str_replace ('&', 'and', $title);

If you are working with accented characters, you want to convert them to us-ascii before the regex, not to loose them (á will become a, ő will become o etc.):

$title = iconv ("UTF-8", "ISO-8859-1//TRANSLIT", $title);

Also your RegEx can be streamlined a bit. This will change consecutive non-alphanumeric chars into a single '-'.

$title = preg_replace ("/[^a-z0-9]+/i", "-", $title);

Also you don't want your title to start or end with a -. The following regex will remove them:

$title = trim ($title, '-');

Upvotes: 2

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

function seo($input){
    $input = str_replace(array("'", "-"), "", $input); //remove single quote and dash
    $input = mb_convert_case($input, MB_CASE_LOWER, "UTF-8"); //convert to lowercase
    $input = preg_replace("#[^a-zA-Z0-9]+#", "-", $input); //replace everything non an with dashes
    $input = preg_replace("#(-){2,}#", "$1", $input); //replace multiple dashes with one
    $input = trim($input, "-"); //trim dashes from beginning and end of string if any
    return $input; //voila
}

Second preg_replace replaces multiple dashes with one.

Examples:

echo seo("Tom's Fish & Chips"); //toms-fish-chips
echo seo("1-2-3 Pizza"); //123-pizza

Upvotes: 10

Roberto Aloi
Roberto Aloi

Reputation: 30985

You can add the following to strip multiple spaces:

$title = preg_replace('/\s+/', ' ', $title);

The following to convert the &:

$title = str_replace('&', 'and', $title);

and you should include the dash in your first regular expression:

$title = preg_replace("/(.*?)([A-Za-z0-9-\s]*)(.*?)/", "$2", $title);

Upvotes: 3

Related Questions