tibberous Posted September 6, 2008 Share Posted September 6, 2008 Is there a better way to do that than substrs? Link to comment https://forums.phpfreaks.com/topic/123066-4564564566-to-456-456-4566/ Share on other sites More sharing options...
steveclondon Posted September 6, 2008 Share Posted September 6, 2008 yes use regular expressions Link to comment https://forums.phpfreaks.com/topic/123066-4564564566-to-456-456-4566/#findComment-635515 Share on other sites More sharing options...
DarkWater Posted September 7, 2008 Share Posted September 7, 2008 Credits to Barand: (with a slight modification) <?php function format_template($str, $template) { $str = str_replace(' ', '', $str); $kt = strlen($template); $ks = strlen($str); $res = ''; $j = 0; for($i=0; $i<$kt; $i++) { if ($j==$ks) break; switch ($c = $template[$i]) { case '#': $res .= $str[$j++]; break; case '!': $res .= strtoupper($str[$j++]) ; break; default: $res .= $c; break; } } return $res; } Usage: <?php $foo = format_template('4564564566', '(###) ###-4566'); echo $foo; ?> Link to comment https://forums.phpfreaks.com/topic/123066-4564564566-to-456-456-4566/#findComment-635541 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.