it will be some numbers contain "," but the end is always 2 letters so its something like this 1234,56ab
Hi, You can add another string in between that will only be the space or pad a space on right of the first string or left for the second string. Cheers, ~Maneet
<?php /* [nannvmen.com] (C)2010-2012 nannvmen.com Inc. This is a freeware $str - your string $num - the end letters num $separation - what you want to add $Id:test.php 2010-09-29 16:04:07Z nannvmen.com $ */ function lick_split( $str,$num, $separation ) { if( empty( $separation ) || empty( $str ) ) { echo 'function lick_split parameter error'; exit; } if( $num > 0 ) { echo 'num should be a negative number!'; exit; } $prefix = substr( $str,0,$num ); $suffix = substr( $str,$num ); return $prefix.$separation.$suffix; } /* eg 1. $newstr = lick_split('my name is sunlicksunlcik',-7,'! NOT '); echo $newstr; exit; */ /* eg 2. $newstr = lick_split('1234ab',-2,',56'); echo $newstr; exit; */ PHP: This is only a simple way,there are many ways. If you have any questions ,feel free to contact me. Regards, Wu
If there will always be 2 letters on the end, you can use substr. <?php $str = '1234,56ab'; $new_str = substr($str, 0, strlen($str) - 2) . ' ' . substr($str, -2); ?> Code (markup):
Regular expression matching will work. This code adds a space when the input has two alpha characters (case-insensitive) appearing last. <?php $strInput = '1234,ab'; preg_match('#(.*)([A-Z]{2,2})#i', $strInput, $strInputArray); $strOutput = $strInputArray[1] . ' ' . $strInputArray[2]; Code (markup):