lostnucleus Posted February 14, 2009 Share Posted February 14, 2009 for example I want to change below string "ThisIsBestPhpHelpSite" (before) to "This Is Best Php Help Site" (after) How can i do that ?? Thanks In advance Quote Link to comment https://forums.phpfreaks.com/topic/145179-how-to-add-a-space-where-letter-goes-capital-in-a-string/ Share on other sites More sharing options...
btherl Posted February 14, 2009 Share Posted February 14, 2009 This almost gets you there: $str = 'ThisIsBestPhpHelpSite'; print preg_replace('/([A-Z])/', ' $1', $str); You'll have an extra space at the front which you can strip off (eg using trim()). Quote Link to comment https://forums.phpfreaks.com/topic/145179-how-to-add-a-space-where-letter-goes-capital-in-a-string/#findComment-761985 Share on other sites More sharing options...
Daniel0 Posted February 14, 2009 Share Posted February 14, 2009 <?php $string = 'ThisIsBestPhpHelpSite'; $newString = preg_replace('#(?<!^)([A-Z])#', ' $1', $string); This will not give you a leading space. Quote Link to comment https://forums.phpfreaks.com/topic/145179-how-to-add-a-space-where-letter-goes-capital-in-a-string/#findComment-761987 Share on other sites More sharing options...
lostnucleus Posted February 14, 2009 Author Share Posted February 14, 2009 Thanks btherl & Daniel0 for your time and solution. Quote Link to comment https://forums.phpfreaks.com/topic/145179-how-to-add-a-space-where-letter-goes-capital-in-a-string/#findComment-762008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.