complex loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luke noob
    New Member
    • Aug 2009
    • 69

    complex loops

    hi my goal is to create a form that grabs all the words and deletes a letter from every combination....

    for example....

    if i submited - Jordan William

    i would want my return result to echo...

    jordan jorda jordn joran jodan jrdan ordan

    william willim willam wiliam wiliam wlliam illiam

    this is only how far i can get ive tryed alot of mixed loops but cant get it to work and now im totally stuck...

    [code=php]


    $words = eregi_replace(" +", " ",$words);

    $indivualWords = explode(' ', $words);




    for($i=0; $i < count($indivual Words); $i++){



    $indivualWords[$i] = substr_replace( $indivualWords[$i],"",-1);


    echo $indivualWords[$i] . "<br />" ;
    }

    }


    [/code]
  • luke noob
    New Member
    • Aug 2009
    • 69

    #2
    even if i changed the -1 to $x-- using another for loop
    some how i need to repeat only $x-- untill the strlen of the word is done then repeat the $i++ when the first word is done, then do the same for the next word and so on, im so confused.

    Comment

    • luke noob
      New Member
      • Aug 2009
      • 69

      #3
      this works but need ta transfer it from hard code into loops as it would need to be flexable depending on how many words in the form and how many letters the word has... any help would be appreciated :)

      this is the code

      [code=php]
      $words = eregi_replace(" +", " ", $words);

      $indivualWords = explode(' ', $words);




      for($i=0; $i < count($indivual Words); $i++){



      $indivualWords1[$i] = substr_replace( $indivualWords[$i],"",-1,1);

      $indivualWords2[$i] = substr_replace( $indivualWords[$i],"",-2,1);

      $indivualWords3[$i] = substr_replace( $indivualWords[$i],"",-3,1);

      $indivualWords4[$i] = substr_replace( $indivualWords[$i],"",-4,1);

      $indivualWords5[$i] = substr_replace( $indivualWords[$i],"",-5,1);


      echo $indivualWords1[$i] . "<br />" ;
      echo $indivualWords2[$i] . "<br />" ;
      echo $indivualWords3[$i] . "<br />" ;
      echo $indivualWords4[$i] . "<br />" ;
      echo $indivualWords5[$i] . "<br />" ;


      }

      }




      }
      [/code]

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        You're almost there!!

        Inside your for loop get the length of $individualWord and create another for loop that traverses this many times. This will allow you to do different-length words.

        Inside this inner for loop, do the letter removal and store each of these combination in a new array.

        I'm curious why you're doing this or what you're doing this for? You do know there are functions that can find similar words based on sound (pronunciations ) or closeness in spelling (order of letters), for example soundex().

        Cheers and Good Luck,

        Dan

        Comment

        • luke noob
          New Member
          • Aug 2009
          • 69

          #5
          thankyou for your help, im am still a bit stuck but got a little further, this is my code now, it works but only for the first word in the form, and does way to many combinations,

          [code=php]

          $words = eregi_replace(" +", " ", $words);

          $indivualWords = explode(' ', $words);




          for($i=0; $i < count($indivual Words); $i++){


          $length = strlen($indivua lWords[$i]);


          for($y=$length; $y > -1; $y--){


          $indivualWords[$y] = substr_replace( $indivualWords[$i],"",$y,1);






          echo $indivualWords[$y] . "<br />" ;

          }
          }
          }

          }

          [/code]

          if i type william jordan in the form and click submit it echo's .....

          william
          willia
          willim
          willam
          wiliam
          wiliam
          wlliam
          illiam
          wlliam
          wllia
          wllim
          wllam
          wliam
          wliam
          liam
          wliam
          wlia
          wlim
          wlam
          wam
          lam
          wlim
          wli
          wl
          wi
          li
          wlim
          wli
          wlm
          wim
          lim
          wliam
          wlia
          wlim
          wlam
          wiam
          liam
          wlliam
          wllia
          wllim
          wllam
          wliam
          wliam
          lliam
          william
          willia
          willim
          willam
          wiliam
          wiliam
          wlliam
          illiam

          im happy and its correct but i only want the first 8...

          william
          willia
          willim
          willam
          wiliam
          wiliam
          wlliam
          illiam

          and also need to edit my script so it looks for the all words on the form.

          i was hoping to learn a lot from this experiment, im hoping that im nearly there but think im going over my head with this :(

          Comment

          Working...