1

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?

Airikr
  • 6,258
  • 15
  • 59
  • 110
  • Why can't you remove spaces with [str_replace](http://php.net/manual/en/function.str-replace.php) and then user [number_format](http://php.net/manual/en/function.number-format.php) or/and [sprintf](http://php.net/manual/en/function.sprintf.php)? – Peon Jul 12 '17 at 15:34
  • I assume you mean why mean why I don't show the numbers like this: 1 403 434 and so on. Because I want it to be just like I showed in my question :) – Airikr Jul 12 '17 at 15:37
  • https://stackoverflow.com/questions/1699958/formatting-a-number-with-leading-zeros-in-php ? – Peon Jul 12 '17 at 15:38

4 Answers4

2
return preg_replace(
    '/^([\s0]+)/', 
    '<span class="color-gray-light">$1</span>', 
    $test
);

This will wrap the zeros (and spaces) in the span you require.

Flosculus
  • 6,880
  • 3
  • 18
  • 42
1

Here's an alternative that doesn't use regex:

function format_str($number, $maxLen){
    $len     = strlen($number);
    $padding = $maxLen - $len;
    if($padding > 0){
        return "<span>".str_pad ('' , $padding, '0', STR_PAD_LEFT)."</span>".$number;
    }

    return $number;
}

See it here: https://3v4l.org/fJHXW

Obviously, this doesn't handle float values. But i assume you're only using it with integers.

Output:

echo format_str(1,6);
// <span>00000</span>1
echo format_str(10,6);
// <span>0000</span>10
echo format_str(152,6);
// <span>000</span>152
echo format_str(32152,6);
// <span>0</span>32152
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
1

I've re-written your function to be a little bit simpler, and more flexible (I hope your site can grow to have content beyond 6 digits!).

Flosculus has the answer to your question (and I've included it in this function); this is just to address your function itself.

<?php
function format_id ($string, $zero_padding) {
    // Add the padding
    $string = str_pad($string, $zero_padding, '0', STR_PAD_LEFT);

    // Now split the string to have space every 3 chars
    $string = chunk_split($string, 3, ' ');

    // Finally, add the span. Credit goes to Flosculus' answer for this!
    return preg_replace(
        '/^([\s0]+)/', 
        '<span class="color-gray-light">$1</span>', 
        $string
    );
}

print format_id("3", 9);
?>
RToyo
  • 2,877
  • 1
  • 15
  • 22
0

This should do the trick:

function format_id($string) {
    return preg_replace('/^([\s0]+)/', '<span class="color-gray-light">$1</span>', $string);
}
Companjo
  • 1,789
  • 18
  • 24