I have this string: 000 003
, which can increase as soon as more comments are published. After 300 comments, this string will look like this: 000 303
. After 1 000 comments, 001 303
and so on. I use chunk_split()
to add the space after the first 3 leading zeros.
I want to replace the leading zeros with <span class="color-gray">the zeros here</span>
but I can't fix it. Here's how the function looks like at the moment:
function format_id($string) {
if($string < 10) {
$test = chunk_split('00000'.$string, 3, ' ');
} elseif($string > 10 AND $string < 100) {
$test = chunk_split('0000'.$string, 3, ' ');
} elseif($string > 100 AND $string < 1000) {
$test = chunk_split('000'.$string, 3, ' ');
} elseif($string > 1000 AND $string < 10000) {
$test = chunk_split('00'.$string, 3, ' ');
} elseif($string > 10000 AND $string < 100000) {
$test = chunk_split('0'.$string, 3, ' ');
} else {
$test = chunk_split($string, 3, ' ');
}
return preg_replace('/^0/', '$1', $test);
}
format_id(35)
returns 00 003
. I want it to look like this in HTML: <span class="color-gray-light">000 00</span>3
. Only the zeros (like I said) will be within the span
element.
How can I solve this problem?
return preg_replace(
'/^([\s0]+)/',
'<span class="color-gray-light">$1</span>',
$test
);
This will wrap the zeros (and spaces) in the span you require.