删除字符串中的空格并将其放在前面

High,

I'm using this function

function special_chars_replace($string){
    $result = preg_replace("/[&%\$#@'\*:\/\"\[\]\{\};\(\)\|\\\=!\^\?`~.,\+-]/", "", $string);
    return $result;
}

to delete all spaces in a css class name.

<?php echo special_chars_replace(strtolower(str_replace(" ","",$itemTags))); ?>

How do i preserve the first space before the name? So i can use it for a css class name. For example: class="tags tag01 tag02"

Just add the space before you echo the string:

<?php 
echo " ".special_chars_replace(strtolower(str_replace(" ","",$itemTags)));
?>

You can use this

<?php echo implode(" ",explode(" ",$itemTags)); ?>

Regular expression is the most effective way to do this.

echo preg_replace(' +', ' ', $itemTags);

What this does is look for one or more spaces (that's what the + does), and replaces it with a single space.

Code typed from memory.