Hyperjase Posted November 19, 2011 Share Posted November 19, 2011 Hiya all, Final one from me for now! I'm trying to add a space into a string, which is 12 digits long, I need to add a space after character number 7, I've looked at the explode syntax but I'm struggling to find a way of counting 7 chars in and add a space, or split the string in two and then just output them with a space in between (which would be easier I think). Thanks, Jason Link to comment https://forums.phpfreaks.com/topic/251451-add-space-after-7-characters/ Share on other sites More sharing options...
Alex Posted November 20, 2011 Share Posted November 20, 2011 There are many ways to do this, one way: $str = '012345678912'; echo substr($str, 0, 7) . ' ' . substr($str, 7); substr Link to comment https://forums.phpfreaks.com/topic/251451-add-space-after-7-characters/#findComment-1289635 Share on other sites More sharing options...
The Little Guy Posted November 20, 2011 Share Posted November 20, 2011 Here is another way: <?php $string = "012345678912"; echo substr_replace($string," ", 7, -strlen($string)); ?> Link to comment https://forums.phpfreaks.com/topic/251451-add-space-after-7-characters/#findComment-1289653 Share on other sites More sharing options...
Hyperjase Posted November 20, 2011 Author Share Posted November 20, 2011 Cracking, thank you, that works like a dream! Just one query on this, how would I check if the space already exists? If it does, not to insert it. Just a fool proof backup. Thanks again, Jason Link to comment https://forums.phpfreaks.com/topic/251451-add-space-after-7-characters/#findComment-1289693 Share on other sites More sharing options...
QuickOldCar Posted November 20, 2011 Share Posted November 20, 2011 <?php $string = trim("012345678912"); if(substr($string,7) != " "){ $string = substr_replace($string," ", 7, -strlen($string)); } echo $string; ?> Link to comment https://forums.phpfreaks.com/topic/251451-add-space-after-7-characters/#findComment-1289709 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.