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
Post a Comment