string - PHP str_replace replace space with dash, but too many dash -


i have string here photo of day - 2011 , need photo-of-the-day-2011 when str_replace, output:

photo-of-the-day---2011 can fix this?

here code:

$albumname = 'photo of day - 2011'; (this comes db , cannot change it) $albumname = str_replace(" ", "-", $albumname); 

you can use more powerful preg_replace, instructing replace runs of dashes and/or spaces single dash:

$name = preg_replace('/[\s-]+/', '-', $name); 

[\s-]+ regular expression matches "one or more of: whitespace , dashes".


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -