Looping Problem - $5 for Resolution

Discussion in 'PHP' started by Infranight, Dec 18, 2010.

  1. #1
    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):
     
    Infranight, Dec 18, 2010 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    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:
     
    tvoodoo, Dec 18, 2010 IP
  3. shofstetter

    shofstetter Well-Known Member

    Messages:
    178
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    120
    #3
    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):
     
    shofstetter, Dec 18, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    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
     
    danx10, Dec 20, 2010 IP