JayMax
JayMax

Reputation: 133

How to handle a dash in your url when replacing spaces with dashes

I currently have my htaccess set as:

RewriteRule ^post/([0-9]+)/([a-zA-Z0-9_-]+) post.php?id=$1

this will display my links as

post/476/title-of-the-page

However if my title has a - in it, it shows three

Title - Of The Page

Becomes

post/476/title---of-the-page

This is my current function for handling links, however I am unsure how to go about this properly

function slug($string, $spaceRepl = "-") {
    // Replace "&" char with "and"
    $string = str_replace("&", "and", $string);
    // Delete any chars but letters, numbers, spaces and _, -
    $string = preg_replace("/[^a-zA-Z0-9 _-]/", "", $string);
    // Optional: Make the string lowercase
    $string = strtolower($string);
    // Optional: Delete double spaces
    $string = preg_replace("/[ ]+/", " ", $string);
    // Replace spaces with replacement
    $string = str_replace(" ", $spaceRepl, $string);
    return $string;
}

I could change my preg_replace to remove -s but some posts use them for different purposes.

Upvotes: 0

Views: 1288

Answers (2)

Samy
Samy

Reputation: 74

You can just replace multiple separator like this :

$string = preg_replace("/-+/", "", $string);

In your function context:

<?php

echo slug("Foo - Bar"); // foo-bar
function slug($string, $spaceRepl = "-") {
    // Replace "&" char with "and"
    $string = str_replace("&", "and", $string);
    // Delete any chars but letters, numbers, spaces and _, -
    $string = preg_replace("/[^a-zA-Z0-9 _-]/", "", $string);
    //delete multiple separator
    $string = preg_replace("/".$spaceRepl."+/", "", $string);
    // Optional: Make the string lowercase
    $string = strtolower($string);
    // Optional: Delete double spaces
    $string = preg_replace("/[ ]+/", " ", $string);
    // Replace spaces with replacement
    $string = str_replace(" ", $spaceRepl, $string);
    return $string;
}

EDIT:

or you can just change your str_replace like this

<?php

echo slug("Foo -    Bar"); // foo-bar
function slug($string, $spaceRepl = "-") {
    // Replace "&" char with "and"
    $string = str_replace("&", "and", $string);
    // Delete any chars but letters, numbers, spaces and _, -
    $string = preg_replace("/[^a-zA-Z0-9 _-]/", "", $string);
    // Optional: Make the string lowercase
    $string = strtolower($string);
    // Optional: Delete double spaces
    $string = preg_replace("/[ ]+/", " ", $string);
    // Replace spaces with replacement
    $string = preg_replace("/\s+/", "-", $string); // new way
    //$string = str_replace(" ", $spaceRepl, $string); // old way
    return $string;
}

Upvotes: 1

Saad Suri
Saad Suri

Reputation: 1382

I made this function for clean slugs. It replaces all multiple dashes to a single one removes all special characters. Maybe helpful for you.

function clean($string) {
    $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
    $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

    return strtolower(preg_replace('/-+/', '-', $string)); // Replaces multiple hyphens with single one.
}

echo clean("Title - Of The Page");

Demo

Note: Perhaps it is not that much optimal so this answer is open for suggestions

Upvotes: 1

Related Questions