Heya Everybody!
I have this string: 67185496000196 (this len is fixed)
I wanna to format it to print: 67.185.496/0001-96
any ideas?? I'm not running PHP4 ...
Thank you!
Egidio
If the length is fixes, just use substring function substr()
print substr($string,1,2).".".substr($string,3,3).".",substr($string,6,3)."/".substr($string,9,4)."-".substr($string,13,2);
There is probably a faster way using regexp though.
CORRECTION 67185496000196 (this len is not fixed) it can be 13, 14, 15 chars long...
it must be from right to left.
any ideas??
Tx. Egidio
If you know how you want to format for each length, you can change the values of the parameters of the substring statements accordingly:
if ($strlen($string)==13) { print bla; }
if ($strlen($string==14) { print bla; };
Ok... here's my go at the problem.. this should work ok i think...
$orginal = "67185496000196";
$new = ereg_replace("([0-9]{2})([0-9]{3})([0-9]{3})([0-9]{4})([0-9]{1,})", "\1.\2.\3/\4-\5", $orginal);
print $new;
Hope that works the way you wanted
Andreas