how to output an array that is inside a for loop and put it in a string twice?

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

    how to output an array that is inside a for loop and put it in a string twice?

    how do i output an array that is inside a for loop and put it in a string twice, the string is inside a function?

    this is my simplified code

    [code=php]

    function myFunction(){

    $words = $_POST['text'];

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

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

    for($x=1; $x < 10; $x++){

    $allwordsTogeth er = $betterwords[$x];

    echo "hi my name is " . $allwordsTogeth er . "it really is " . $allwordsTogeth er . "<br />";

    }

    }

    echo myFunction();

    [/code]

    i would like it to echo for exapmle

    hi my name is bob johnson it really is bob johnson
    Last edited by luke noob; Oct 12 '10, 02:51 PM. Reason: made a mistake
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Can you point out where your script is broken or not working?

    Dan

    Comment

    • luke noob
      New Member
      • Aug 2009
      • 69

      #3
      thankyou for the reply

      its looping the string and just one array value per string then moving on to the next

      like this...

      hi my name is bob it really is bob

      hi my name is johnson it really is johnson

      i would like it like this....

      hi my name is bob johnson it really is bob johnson

      Comment

      • JKing
        Recognized Expert Top Contributor
        • Jun 2007
        • 1206

        #4
        Trying to make some sense of this here but why do you explode the text just to put it back together again?

        Why not just echo $words after you have run your regular expression on it?

        Comment

        • luke noob
          New Member
          • Aug 2009
          • 69

          #5
          because i would like to add a sign or other letters like....
          [code=php]
          $allwordsTogeth er = $betterwords . "!";

          echo "i can shout " . $allwordsTogeth er . "i really can talk loud " . $allwordsTogeth er . "<br />";

          [/code]

          i can shout bob!johnson!ste ve!hello!people ! i really can talk loud bob!johnson!ste ve!hello!people !

          this is a simplified version of what i am doing but has the same outcome. i think the problem is that the because the string is inside the "for" loop it repetes the string aswell, i would like to know how to take the string out of the loop to stop repeting it but i need to echo out the array in side the string still. please help. :(

          Comment

          • JKing
            Recognized Expert Top Contributor
            • Jun 2007
            • 1206

            #6
            Code:
            $allwordsTogether = "";
             for($x=1; $x < 10; $x++){
              
              $allwordsTogether += $betterwords[$x];  
             }
             echo "hi my name is " . $allwordsTogether . "it really is " . $allwordsTogether . "<br />";
            Give that a try.

            Comment

            • luke noob
              New Member
              • Aug 2009
              • 69

              #7
              This is my actual code for better understanding.. .
              [code=php]
              function myWeirdSearchFu nction(){

              if($_POST['submit'] && !empty($_POST['words'])){

              $wordsFromForm = $_POST['words'];

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

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

              $AllindivualWor ds = "";


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



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


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


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




              echo "hi my name is " . $AllindivualWor ds . " then some more text. <br />";







              }





              }

              }

              }
              [/code]

              This prints....

              hi my name is jason then some more text.
              hi my name is jaso then some more text.
              hi my name is jasn then some more text.
              hi my name is jaon then some more text.
              hi my name is json then some more text.
              hi my name is ason then some more text.
              hi my name is essex then some more text.
              hi my name is esse then some more text.
              hi my name is essx then some more text.
              hi my name is esex then some more text.
              hi my name is esex then some more text.
              hi my name is ssex then some more text.

              How can i get......

              hi my name is jasonjasojasnja onjsonasonessex esseessxesexese xssex then some more text.

              please.

              Comment

              • JKing
                Recognized Expert Top Contributor
                • Jun 2007
                • 1206

                #8
                I apologize I gave you the wrong operator previously. The += operator should have been .= for strings.

                The .= operator is a shorter way to express the following
                Code:
                //Long way
                $mystring = $mystring . $otherstring;
                
                //Short way
                $mystring .= $otherstring;
                Here I updated your code.
                Code:
                function myWeirdSearchFunction(){  
                
                	if($_POST['submit'] && !empty($_POST['words'])){
                		$wordsFromForm = $_POST['words']; 
                		$wordsFromForm = eregi_replace(" +", " ", $wordsFromForm);  
                		$indivualWords = explode(' ', $wordsFromForm);
                		$AllindivualWords = "";    
                		for($i=0; $i < count($indivualWords); $i++){ 
                			$length = strlen($indivualWords[$i]);
                			for($y=$length; $y > -1; $y--){
                				$AllindivualWords .= substr_replace($indivualWords[$i],"",$y,1); 
                			} 
                		}
                		echo "hi my name is " . $AllindivualWords . " then some more text. <br />";  
                	}
                }

                Comment

                • luke noob
                  New Member
                  • Aug 2009
                  • 69

                  #9
                  That works great, i still dont understand what that operater does but ill look more into it. Thank you JKing.

                  Comment

                  Working...