Guest Posted May 26, 2010 Share Posted May 26, 2010 i'm looking for a function or a part of code that would add a character before each word in a text "this is a text" would become "+this +is +a +text" can anyone please help me ? thanks ! Link to comment https://forums.phpfreaks.com/topic/202914-add-a-character-before-each-word/ Share on other sites More sharing options...
Bladescope Posted May 26, 2010 Share Posted May 26, 2010 function addToWords($string) { $array = split(' ', $string); foreach($array as $key=>$value) { $array[$key] = '+'.$array[$key]; } $return = implode (' ', $array); } Probably an easier way of doing it, best I could think of off the top of my head (without going into regex) Link to comment https://forums.phpfreaks.com/topic/202914-add-a-character-before-each-word/#findComment-1063365 Share on other sites More sharing options...
kenrbnsn Posted May 26, 2010 Share Posted May 26, 2010 A slightly easier way: <?php $str = 'This is a test of adding a plus sign before each word.'; $str = '+' . implode(' +',explode(' ',$str)); echo $str; ?> Ken Link to comment https://forums.phpfreaks.com/topic/202914-add-a-character-before-each-word/#findComment-1063368 Share on other sites More sharing options...
Guest Posted May 26, 2010 Share Posted May 26, 2010 thanks a lot! Link to comment https://forums.phpfreaks.com/topic/202914-add-a-character-before-each-word/#findComment-1063378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.