please help me with my small program

Discussion in 'PHP' started by stars, Jan 26, 2007.

  1. #1
    I want to create a small program like this.
    For example, I have this text
    when the program run, I put this text in to the box. After my program run, I get this text
    What did the program do?
    Just put a"," between the words.

    anyone can help me witht his code.I reallly need it for my project.

    (optional)second, this code can do is check the words.If any word is repeated then delete that word,just keeps the original word.

    This is so great if you can help me with this code.thanks
     
    stars, Jan 26, 2007 IP
  2. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:

    <?php
      $words = array('word1', 'word2', 'word3', 'word1', 'word4');
      $unique = array_unique($word);
      echo implode(', ', $unique)
    ?>
    PHP:
    Cheers! :)
     
    picouli, Jan 26, 2007 IP
  3. stars

    stars Peon

    Messages:
    42
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks, but this is not what I want, My code have a box to put the text , and another box to get the result.
     
    stars, Jan 26, 2007 IP
  4. sethuhdiah

    sethuhdiah Well-Known Member

    Messages:
    1,651
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    130
    #4
    $pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
    $pieces = explode(" ", $pizza);
    foreach($pieces as $piece){
    echo $piece.", ";
    }

    this should return: piece1, piece2, piece3, piece4, piece5, piece6,
     
    sethuhdiah, Jan 26, 2007 IP
  5. PureLuckk

    PureLuckk Peon

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No, I think what he is needing is something like this.

    This script takes a form field called "StringOfWords" and places it in the $Words var. It then takes those words and puts them into an array. From there it creates a blank array to be used later by adding distinct words to it to determine if we have multiple words of the same. If we have multiple it will skip and move to the next word. Next we echo the word then if we are that far as echo'ing the word then we add it to the list of used words.

    Simple, eh?

    
    <?php
    // $Words = "mike mike mike glen hello hello hello hello glen mike pure pure luck yes yes no";
    $Words = $_POST['StringOfWords'];
    $ArrayOfWords = explode(" ", $Words);
    $ArrayOfUsedWords = array(); /* Setting up an empty array so we can 'push' words into it to determine if we have came across the word before. */
    
    // Echo each Word.
    foreach($ArrayOfWords AS $Word)
    {
    	// Is word already been used?
    	if(!in_array($Word, $ArrayOfUsedWords))
    	{
    		echo $Word . ", ";
    		array_push($ArrayOfUsedWords, $Word); /* Pushing the word to the array of used words. */
    	}
    }
    ?>
    PHP:
    Good luck using it!
     
    PureLuckk, Jan 27, 2007 IP
  6. stars

    stars Peon

    Messages:
    42
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks. This works well, but not perfect, When the space between the words is more than 1, then the first comma is double

    mike mike mike glen hello [5 spaces here] hello hello[3 spaces here] hello glen mike pure pure luck yes yes no
    I think I can fix this.thank you so much.You are so great
     
    stars, Jan 27, 2007 IP
  7. jgarrison

    jgarrison Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This will do it:

    <?php
      if($_POST["action"] == "submit"){
      	$words = split(' ', preg_replace('/(\s)\s+/', '\\1', $_POST["inputString"]));
      	$outputString = implode(', ', array_unique($words));
      }
    ?>
    <form method="post">
    	Input: <input name="inputString" type="text" /><br />
    	<input name="action" type="hidden" value="submit" />
    	<input type="submit" value="Submit" /><br /><br />
    	Output: <input name="output" value="<?=$outputString ?>" />
    </form>
    Code (markup):
     
    jgarrison, Jan 27, 2007 IP
  8. stars

    stars Peon

    Messages:
    42
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks this one is great.
     
    stars, Jan 30, 2007 IP