Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts

    String manipulations

    Hi,

    I get a string from a form, and I need to manipulate it, and create an array containing all the results.

    - If the string has 1 word, the array should contain 1 element with the same word.
    - If the string has 2 words, the array should contain 2 elements, looking like this: [0] => "word1word2", [1] => "word1-word2"
    - If the string has 3 words, the array should contain 4 elements, looking like this:
    Code:
    [0] => "word1word2word3", [1] => "word1-word2word3", [2] => "word1word2-word3", [3] => "word1-word2-word3"
    - If the string has 4 words, the array should contain 8 elements, looking like this:
    Code:
    [0] => "word1word2word3word4", [1] => "word1-word2word3word4", [2] => "word1word2-word3word4", [3] => "word1word2word3-word4", [4] => "word1-word2-word3word4", [5] => "word1-word2word3-word4", [6] => "word1word2-word3-word4", [7] => "word1-word2-word3-word4"
    - etc.

    Hope you get the point...
    I'm trying to figure out a to handle this, buy I can't think of how to do it.
    What do you guys think? Any help would be appreciated.

    Thanks.

  • #2
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Try this:
    PHP Code:
    <?php
    function doIt($str)
    {
        
    $words preg_split('#\s+#'$str); // split the string at spaces to get a word array
        
    $lastWord array_pop($words); // the last word needs special handling because it does never get a `-` appended
        
    $wordArr = array('');
        for(
    $i 0$count count($words); $i $count; ++$i)
        {
            
    $wordArr array_merge($wordArr$wordArr); // double the size of the array for each word and use what we've already constructed
            
    foreach($wordArr as $key => $wordStr)
            {
                if(
    $key < (1<<$i)) // to append or not to append - that's the question
                
    {
                    
    $wordArr[$key] .= $words[$i];
                }
                else
                {
                    
    $wordArr[$key] .= $words[$i].'-';
                }
            }
        }
        foreach(
    $wordArr as &$wordStr// append the last word to all strings
        
    {
            
    $wordStr .= $lastWord;
        }
        return 
    $wordArr;
    }
    print_r(doIt('word1 word2    word3 word4'));
    ?>
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Must be a php5 thang, but I had to change that lastword part. This worked for me.
    PHP Code:
    function doIt($str)
    {
        
    $words preg_split('#\s+#'$str); // split the string at spaces to get a word array
        
    $lastWord array_pop($words); // the last word needs special handling because it does never get a `-` appended
        
    $wordArr = array('');
        for(
    $i 0$count count($words); $i $count; ++$i)
        {
            
    $wordArr array_merge($wordArr$wordArr); // double the size of the array for each word and use what we've already constructed
            
    foreach($wordArr as $key => $wordStr)
            {
                if(
    $key < (1<<$i)) // to append or not to append - that's the question
                
    {
                    
    $wordArr[$key] .= $words[$i];
                }
                else
                {
                    
    $wordArr[$key] .= $words[$i].'-';
                }
            }
        }
        foreach(
    $wordArr as $wordStr// append the last word to all strings
        
    {
            
    $final_words[] .= $wordStr.$lastWord;
        }
        return 
    $final_words;


  • #4
    Regular Coder
    Join Date
    Aug 2006
    Posts
    135
    Thanks
    0
    Thanked 0 Times in 0 Posts
    dumpfi - thanks a lot, works like a charm


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •