can anyone help me with the code below, I would like to strip out comas from the URL,-drag-link-end/am1148

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

Recommended Answers

All 3 Replies

use str_replace like this

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

this code removes all commas from the variable

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);

Member Avatar

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. 
//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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.