3
Contributors
3
Replies
17
Views
3 Years
Discussion Span
Last Post by diafol
1

use str_replace like this

$urlkey = str_replace( ",", "", $str );

this code removes all commas from the variable

Edited by diafol: reformatted code

0

like so?

1.$str = trim($str);/remove any unwanted outer spacing/
2.$urlkey = preg_replace("/[^0-9a-zA-Z]+/","-",strtolower($str));
urlkey = str_replace( ",", "", $str );
3.$htmlkey = ($htmlext) ? $urlkey.".html" : "";
4.return array($urlkey,$htmlkey);

0

I've seen the 'slugify' regex before and I'm not convinved it's that great.

An approach may be...

$str = 'The new Bulgarian president (президент), Rosen Plevneliev says "Yes!"';

//Add replacements as you see fit. 
//Replacements:
//1. Replace all whitespace (single or multiple) instances or an underscore by a single dash
//2. Delete all characters that are not alphanumeric nor dashes
//3. Replace multiple dashes by a single dash

//This could be developed further to include transliteration if you were going to use
// accented or non-Latin alphanumerics

function urlify($string)
{
    $patterns = array('/\s+|_/', '/[^0-9a-zA-Z\-]+/', '/[\-]+/');
    $replacements = array('-', '', '-');

    $lower = mb_strtolower(trim($string),'UTF-8');
    return preg_replace($patterns,$replacements,$lower);
}

echo urlify($str);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.