jesushax Posted March 27, 2009 Share Posted March 27, 2009 hi say i have a value "JohnSmith" would it e possible to change with code to "John Smith" there needs to be no sapces in teh db field name as its used for other scripts but i would like to display it on pages with a space THanks Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/ Share on other sites More sharing options...
Mark Baker Posted March 27, 2009 Share Posted March 27, 2009 $string = "JohnSmith"; $splitStringArray = preg_split('/([A-Z])/',$string,-1,PREG_SPLIT_NO_EMPTY+PREG_SPLIT_DELIM_CAPTURE); $splitString = ''; for($i = 0; $i < count($splitStringArray); $i++) { if (($i > 0) && (($i % 2) == 0)) $splitString .= ' '; $splitString .= $splitStringArray[$i]; } echo $splitString; Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795011 Share on other sites More sharing options...
jesushax Posted March 27, 2009 Author Share Posted March 27, 2009 that didnt work :| in dreamweaver PREG_SPLIT_NO_EMPTY+PREG_SPLIT_DELIM_CAPTURE); didnt show up as an colour could something be wrong there? Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795021 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 function addSpaces($str) { $tmp=''; $len=strlen($str); for ($i=0;$i<$len;++$i) { $tmp.=($str[$i]>='A'&&$str[$i]<='Z'&&$i>0 ? ' ' : '').$str[$i]; } return $tmp; } Not tested, off the top of my head. EDIT: Changed || to && Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795031 Share on other sites More sharing options...
jesushax Posted March 27, 2009 Author Share Posted March 27, 2009 sorry didnt work again Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795039 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 I just tested it and it works. <?php echo addSpaces('JoeBloggs'); function addSpaces($str) { $tmp=''; $len=strlen($str); for ($i=0;$i<$len;++$i) { $tmp.=($str[$i]>='A'&&$str[$i]<='Z'&&$i>0 ? ' ' : '').$str[$i]; } return $tmp; } ?> In my browser I got Joe Bloggs Did you see the edit I made after posting? Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795040 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 I also just tested it with capitals text to each other: echo addSpaces('JoeBloggsLikesToEatAPlatterOfFish'); see "AP" together? I got this: Joe Bloggs Likes To Eat A Platter Of Fish Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795041 Share on other sites More sharing options...
Mark Baker Posted March 27, 2009 Share Posted March 27, 2009 that didnt work :| in dreamweaver PREG_SPLIT_NO_EMPTY+PREG_SPLIT_DELIM_CAPTURE); didnt show up as an colour could something be wrong there? Did you actually try running it, or simply take dreamweaver's non-higjlighting as a sign that it wouldn't work Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795051 Share on other sites More sharing options...
jesushax Posted March 27, 2009 Author Share Posted March 27, 2009 yes i tested it regardless of dreamweavers non colouring, im not stupid lol ah yes it did work i had my functions.php include lower then the text i wanted to run the addSpaces function on moved it to the top and its all good now thankyou very much for your time Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795054 Share on other sites More sharing options...
Mark Baker Posted March 27, 2009 Share Posted March 27, 2009 yes i tested it regardless of dreamweavers non colouring, im not stupid lol ah yes it did work In that case, be aware that it won't work with single letter words such as the A in Yesideez's example JoeBloggsLikesToEatAPlatterOfFish Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795057 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 yes i tested it regardless of dreamweavers non colouring, im not stupid lol ah yes it did work In that case, be aware that it won't work with single letter words such as the A in Yesideez's example JoeBloggsLikesToEatAPlatterOfFish I think you'll find I tested it and it worked!!! Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795060 Share on other sites More sharing options...
Mark Baker Posted March 27, 2009 Share Posted March 27, 2009 I think you'll find I tested it and it worked!!! I was actually thinking about my preg_split() code, which doesn't handle that situation. Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795063 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 Ah! I did originally think along the lines of a regex string then realised regex doesn't like me so I went for the char-by-char method. Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795064 Share on other sites More sharing options...
Mark Baker Posted March 27, 2009 Share Posted March 27, 2009 <slaps self on forehead with a wet fish> $splitStringReplace = trim(preg_replace('/([A-Z])/',' ${1}',$string)); Quote Link to comment https://forums.phpfreaks.com/topic/151362-solved-split-name-by-captial-letters/#findComment-795066 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.