Zend - The PHP Company




Utilities

Add Code


Comma integer  

Type: code fragment
Added by: cwe7g
Entered: 30/03/2002
Last modified: 04/12/2001
Rating: - (fewer than 3 votes)
Views: 4401
Adds commas to an integer


<?php
// splits a string and inserts comma to create an integer look
function comma_int$string )
  {
  
$length strlen$string );
  if( 
$length <= )
    {
    return 
$string;
    }
  else
    {
    
$loop_count intval( ( $length ) );
    
$section_length = -3;
    for( 
$i 0$i $loop_count$i++ )
      {
      
$sections[$i] = substr$string$section_length);
      
$section_length $section_length 3;
      }

    
$stub = ( $length );   
    if( 
$stub != )
      {
      
$sections[$i] = substr$string0$stub );
      }

    return 
implode","array_reverse$sections ) );
    }
  }

?>


Usage Example


$hits = 1234567;

echo comma_int( $hits );


Rate This Script





Search



This Category All Categories