a small string question

Discussion in 'PHP' started by dizyn, Mar 6, 2008.

  1. #1
    I have a sting like "03335240141" i want display it like 0333-524-0141 is there any string function that can do this for me?

    thank you
    dizyn
     
    dizyn, Mar 6, 2008 IP
  2. Jawn

    Jawn Peon

    Messages:
    200
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can do it with simple regex code.


    
    <?php
    
    	$number = "03335240141";
    	$pattern = "/([0-9]{4})([0-9]{3})([0-9]{4})/";
    
    	preg_match($pattern, $number, $matches);
    
    	echo $matches[1]. "-" .$matches[2]. "-" .$matches[3];
    	
    ?>
    
    PHP:
     
    Jawn, Mar 6, 2008 IP