freephoneid Posted July 24, 2008 Share Posted July 24, 2008 Hi, Can anyone tell me how to convert a given string in the below format in 1 shot in PHP? Given String: MT9L2BGQGJC547VN4JT4Z8WY9FKFGZC Expected: MT-9L2B-GQGJC-547VN-4JT4Z-8WY9F-KFGZC Can anyone tell me how to do it using better way may be using Regualr expression in PHP. Thanks! Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/ Share on other sites More sharing options...
genericnumber1 Posted July 25, 2008 Share Posted July 25, 2008 There's probably an easier way, but.... <?php function format($str) { $str = preg_split('%%', $str, -1, PREG_SPLIT_NO_EMPTY); $newstr = ''; $length = count($str); for($i = 0; $i < $length; ++$i) { $char = array_shift($str); if($i == 2 || $i == 6 || ((count($str) + 1) % 5 == 0 && $i != 1)) { $newstr .= '-' . $char; } else { $newstr .= $char; } } return $newstr; } echo format('MT9L2BGQGJC547VN4JT4Z8WY9FKFGZC'); ?> Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/#findComment-599089 Share on other sites More sharing options...
freephoneid Posted July 25, 2008 Author Share Posted July 25, 2008 Hi, Thanks for the great code...but if I enter "AFDSGDGDK", it gives wrong result....it should give "AF-DSGD-GDK" The format is 2 Chars - 4 Chars - 5 chars - 5 Chars - 5 chars - 5 chars - 5 chars Can you tell me how to fix that? Thanks again! Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/#findComment-599093 Share on other sites More sharing options...
genericnumber1 Posted July 25, 2008 Share Posted July 25, 2008 Hi, Thanks for the great code...but if I enter "AFDSGDGDK", it gives wrong result....it should give "AF-DSGD-GDK" The format is 2 Chars - 4 Chars - 5 chars - 5 Chars - 5 chars - 5 chars - 5 chars Can you tell me how to fix that? Thanks again! Ah, thought you meant there was a set length of characters (what you had originally). It won't work for any character lengths different than what you put earlier. Unfortunately I don't have the time to fix it to work for any string length right now. Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/#findComment-599095 Share on other sites More sharing options...
freephoneid Posted July 25, 2008 Author Share Posted July 25, 2008 Can any one help me correct above code? I'm a newbe to php n regular expressions. Thanks in Advance! Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/#findComment-599104 Share on other sites More sharing options...
homer.favenir Posted July 25, 2008 Share Posted July 25, 2008 try this: <pre> <?php $str = 'MT9L2BGQGJC547VN4JT4Z8WY9FKFGZC'; $str1 = substr($str,0,2); $str2 = substr($str,2,4); $str3 = substr($str,6,5); $str4 = substr($str,11,5); $str5 = substr($str,16,5); $str6 = substr($str,21,5); $str7 = substr($str,26,5); echo $str1."-".$str2."-".$str3."-".$str4."-".$str5."-".$str6."-".$str7; ?> </pre> Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/#findComment-599160 Share on other sites More sharing options...
effigy Posted July 25, 2008 Share Posted July 25, 2008 <pre> <?php $tests = array( 'MT9L2BGQGJC547VN4JT4Z8WY9FKFGZC', 'AFDSGDGDK', ); foreach ($tests as $test) { if (!preg_match('/\A(.{2})(.{4})(.+)/', $test, $pieces)) { die('Invalid format'); } array_shift($pieces); $pieces[2] = join( '-', preg_split( '/(.{5})/', $pieces[2], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ) ); echo join('-', $pieces); echo '<hr>'; } ?> </pre> Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/#findComment-599511 Share on other sites More sharing options...
freephoneid Posted July 25, 2008 Author Share Posted July 25, 2008 Thanks Everyone!!!!!!!!!! This Forum is GREAT!!!!!!! Can you also explain a little what the below line does exactly? preg_match('/\A(.{2})(.{4})(.+)/', $str, $pieces); ---> Here, what is A stands for? preg_split('/(.{5})/', $pieces[2], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); ---> Here, what is PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY?? Thanks!!!! Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/#findComment-599650 Share on other sites More sharing options...
freephoneid Posted July 25, 2008 Author Share Posted July 25, 2008 Hi effigy, I just tried your code & unfortunately it does not work for 2 characters string input. If I enter "GH", then ideally it should return "GH" only but its not working... Can anyone fix it as well?? Appreciate your help & thanks again for all resopnses. Thanks! Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/#findComment-599707 Share on other sites More sharing options...
genericnumber1 Posted July 25, 2008 Share Posted July 25, 2008 Finally got time to fix my old code now... <?php $str = 'ALDABSDDFEFE'; $strLength = strlen($str); $newstr = substr($str, 0, 2); if($strLength > 2) { $newstr .= '-' . substr($str, 2, 4); } if($strLength > 6) { $remainingStr = substr($str, 6); $remainingStr = preg_split('%%', $remainingStr, -1, PREG_SPLIT_NO_EMPTY); $remainingChars = count($remainingStr); for($i = 0; $i < $remainingChars; ++$i) { $char = array_shift($remainingStr); if($i % 5 == 0) { $newstr .= '-' . $char; } else { $newstr .= $char; } } } echo $newstr; ?> Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/#findComment-599740 Share on other sites More sharing options...
sasa Posted July 30, 2008 Share Posted July 30, 2008 try <?php $test = 'MT9L2BGQGJC547VN4JT4Z8WY9FKFGZC'; $part = array(); $part[] = substr($test,0,2); $test =substr($test,2); $part[] = substr($test,0,4); $test =substr($test,4); while (strlen($test)){ $part[] = substr($test,0,5); $test =substr($test,5); } echo $out = implode('-', $part); ?> Link to comment https://forums.phpfreaks.com/topic/116498-php-regular-expression/#findComment-603378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.