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
Try this: <?php $words = array('word1', 'word2', 'word3', 'word1', 'word4'); $unique = array_unique($word); echo implode(', ', $unique) ?> PHP: Cheers!
thanks, but this is not what I want, My code have a box to put the text , and another box to get the result.
$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,
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!
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
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):