views:

207

answers:

2

Hello,

I would like to sanitize a string in to a URL so this is what I basically need.

  1. Everything must be removed except alphanumeric characters and spaces and dashed.
  2. Spaces should be converter into dashes.

Eg.

This, is the URL!

must return

this-is-the-url

Thanks

+2  A: 
function slug($z){
    $z = strtolower($z);
    $z = preg_replace('/[^a-z0-9 -]+/', '', $z);
    $z = str_replace(' ', '-', $z);
    return trim($z, '-');
}
SilentGhost
great thanks.. Just one edit.. I want to remove dashes from beginning and end before returning $z just in case they exists.
atif089
@atif089: added
SilentGhost
why the downvote?
SilentGhost
-1: Reading between the lines of what SilentGhost *intends* rather than the code he/she has written. this appears url-safe, it's at the cost of loss of information. The right way to encode data for a URL is to use urlencode().
symcbean
(I see it does the translation shown in the example - but not what atif089 asked for)
symcbean
@symcbean: most people on board of this planetary ship are not comfortable `urldecode`-ing on the fly.
SilentGhost
@symcbean urlecode is not what I needed because I want to eliminate symbols rather than converting them. So this is exactly what I wanted.
atif089
shorter: strtolower(trim(preg_replace("/[^\w]+/", "-", $z), "-"))
mario
@mario: 1. it doesn't do the same processing; 2. it's a maintenance nightmare.
SilentGhost
A: 

First strip unwanted characters

$new_string = preg_replace(“/[^a-zA-Z0-9\s]/”, “”, $string);

Then changes spaces for unserscores

$url = preg_replace('/\s/', '-', $new_string);

Finally encode it ready for use

$new_url = urlencode($url);
Rooneyl
underscore is a different character: `_` is an underscore, `-` is a hyphen. Also using `urlencode` on such a string doesn't change anything. You're also forgetting hypen in the first regex and `\s` is not equivalent to space character.
SilentGhost