Re: formatting a string

From: Date: Sun, 25 Apr 2004 05:15:25 +0000
Subject: Re: formatting a string
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-184545@lists.php.net to get a copy of this message
[snip] $phone = '1234567890'; $newphone = preg_replace('/(\d{3})(\d{3})(\d)/','(\1)\2-\3',$phone); echo $newphone.'<br>'; -- regards, Tom [/snip] ok sorry but since i never used preg_* before i dont quite get what some of this stuff means. i looked at the doc page for it but it doesnt make mention at all of what \d, \w, \s or any of those things mean... i only assume that \d means digit and \w or \s means blank space?? anyways to go through the whole example above part by part: $phone = '1234567890';//understand that $newphone = preg_replace(//ok now what does this stuff //mean?? '/(\d{3})(\d{3})(\d)/' im gathering the line above is the search string (what to look for)? if so i get from it that it is supposed to look for the first block of 3 digits then the second block of 3 digits and the other 4 numbers by themself in a sense seperating the string into 3 different parts: 123 456 7890 and then asigning like "id numbers to the blocks" '(\1)\2-\3' and this one above says put block 1 between (). take block 2 and put a - after it and leave the other 4 numbers alone to come up with: (123)456-7890 $phone); the original number to do the replace on of course let me know if i got that set right

« previous php.general (#184545) next »