plznty Posted December 1, 2009 Share Posted December 1, 2009 I want two words to be joined into one like this: $word1 = "ABC"; $word2 = "123"; OUTPUT = A1B2C3 Thanks Link to comment https://forums.phpfreaks.com/topic/183554-how-do-you-merge-words/ Share on other sites More sharing options...
Deoctor Posted December 1, 2009 Share Posted December 1, 2009 i think u can use the fgetc() for this one.. Link to comment https://forums.phpfreaks.com/topic/183554-how-do-you-merge-words/#findComment-968820 Share on other sites More sharing options...
Daniel0 Posted December 1, 2009 Share Posted December 1, 2009 Something like this should do it: <?php $word1 = "ABC"; $word2 = "123"; $res = ''; for ($i = 0, $l1 = strlen($word1), $l2 = strlen($word2); $i < $l2 && $i < $l1; ++$i) { if ($i < $l1) { $res .= $word1[$i]; } if ($i < $l2) { $res .= $word2[$i]; } } echo $res; Link to comment https://forums.phpfreaks.com/topic/183554-how-do-you-merge-words/#findComment-968833 Share on other sites More sharing options...
plznty Posted December 1, 2009 Author Share Posted December 1, 2009 Wow thanks, can you make it so it can revert the $res back into the variables? Link to comment https://forums.phpfreaks.com/topic/183554-how-do-you-merge-words/#findComment-968845 Share on other sites More sharing options...
Daniel0 Posted December 1, 2009 Share Posted December 1, 2009 Yes I can. Read the string character by character. You put every second character in one variable and the others in another variable. Why don't you try it out? Link to comment https://forums.phpfreaks.com/topic/183554-how-do-you-merge-words/#findComment-968849 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.