I am creating a loop that breaks apart words separated with hyphens, adds an ha, then puts it together. E.g. "put-it" would be "putha-itha" Anyways, with the code below, it "put-it" does become "putha-itha", but if I enter "put-it goot-it" it becomes " putha-itha putha-itha-gootha-itha", so I can't have more than one. "put-it goot-it soot-it" becomes " putha-itha putha-itha-gootha-itha putha-itha-gootha-itha-gootha-ita". Can anybody help me with this problem? Willing to pay $5 PayPal for a fix. $words = explode('-', $word); foreach ($words as $hword) { $hnewPhrase[] = $hword.'ha'; } $hstring = implode('-', $hnewPhrase); $newPhrase[] = $hstring.$punc; Code (markup):
Here you go : <?php $words = 'put-it goot-it soot-it'; $word = explode(" ",$words); foreach($word as $key => $value) { if(strpos($value,"-") !== false) { $word2 = explode("-",$value); foreach($word2 as $key2 => $value2) { $word2[$key2] = $value2.'ha'; } $word[$key] = implode("-",$word2); } } $words = implode(" ",$word); echo $words; ?> PHP:
try this your list can be as long as you want it to be <?php $phrase = "put-it goot-it soot-it"; echo "\n\n".change_it($phrase)."\n\n"; function change_it($phrases){ $phrase_list = explode(' ',$phrases); foreach($phrase_list as $phrase){ $words[] = explode('-',$phrase); } $i = 0; foreach($words as $word_set){ foreach($word_set as $word){ $new_words[$i][] = $word.'ha'; } $i++; } $j = 0; foreach($new_words as $new_word){ $final_words[$j] = implode('-',$new_word); $j++; } $done = implode(' ',$final_words); return ($done); } ?> Code (markup):
Why do you need to loop? <?php $str = 'put-it and put-it goot-it'; $suffix = 'ha'; echo preg_replace('#\b([^ ]+)\-([^ ]+)\b#', "$1{$suffix}-$2{$suffix}", $str); ?> PHP: If you wish to donate send to: http://www.digitalpoint.com/tools/donate.html