spires Posted July 31, 2008 Share Posted July 31, 2008 Hi Guys I trying to create a function that allows me to remove duplicate words. http://www.adwordstool.co.uk/duplicate.php As you can see from the form, if you enter more than one word, only one word is displayed in the textarea. For example, if I enter: one two three only 'one' is visible in the text area. But, if I echo $out; all three words appear in the top left corner. Here is the code: $adgroup = sanitize($_POST['adgroup']); $adgroup_array = explode("\n", $adgroup); if($adgroup) { // generate results string and assign to template $smarty->assign("adgroup_text", generate_adgroup_text($adgroup_array)); } $smarty->assign('adgroup', $adgroup); function generate_adgroup_text(&$adgroup_array) { $out = ""; // init results string to return $adgroup_array_new = array_unique($adgroup_array); foreach ($adgroup_array_new as $keyword) { $keyword = trim($keyword); $out = "$keyword\n"; echo $out; // see top left corner } return $out; // return results string } Thanks for any help Link to comment https://forums.phpfreaks.com/topic/117553-help-with-looping/ Share on other sites More sharing options...
rhodesa Posted July 31, 2008 Share Posted July 31, 2008 you need to APPEND to $out: $out .= "$keyword\n"; Link to comment https://forums.phpfreaks.com/topic/117553-help-with-looping/#findComment-604601 Share on other sites More sharing options...
rhodesa Posted July 31, 2008 Share Posted July 31, 2008 together the code would look like: <?php $adgroup = sanitize($_POST['adgroup']); $adgroup_array = explode("\n", $adgroup); if($adgroup) { // generate results string and assign to template $smarty->assign("adgroup_text", generate_adgroup_text($adgroup_array)); } $smarty->assign('adgroup', $adgroup); function generate_adgroup_text($adgroup_array) { $out = ""; // init results string to return foreach (array_unique($adgroup_array) as $keyword) { $keyword = trim($keyword); $out .= "$keyword\n"; } return $out; // return results string } ?> Link to comment https://forums.phpfreaks.com/topic/117553-help-with-looping/#findComment-604605 Share on other sites More sharing options...
ainoy31 Posted July 31, 2008 Share Posted July 31, 2008 Have you tried using the array_unique()? This should eliminate any duplicate values from the array. Link to comment https://forums.phpfreaks.com/topic/117553-help-with-looping/#findComment-604608 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 I'd use something like this ( assuming a multi-line string... you can simply use explode for a single line ) <?php $string = <<<HEREDOC At least one of many large, lake-like features on Saturn's moon Titan contains liquid hydrocarbons, making it the only body in the solar system besides Earth known to have liquid on its surface, NASA said Wednesday. An image of Titan's surface shows what scientists believe are bodies of liquid, shown in blue. An image of Titan's surface shows what scientists believe are bodies of liquid, shown in blue. Scientists positively identified the presence of ethane, according to a statement from NASA's Jet Propulsion Laboratory in Pasadena, which manages the international Cassini spacecraft mission exploring Saturn, its rings and moons. Liquid ethane is a component of crude oil. Cassini has made more than 40 close flybys of Titan, a giant planet-sized satellite of the ringed world. Scientists had theorized that Titan might have oceans of methane, ethane and other hydrocarbons, but Cassini found hundreds of dark, lake-like features instead, and it wasn't known at first whether they were liquid or dark, solid material, JPL's statement said. HEREDOC; # Strip some punctuation $punc = array( '.', ',', ';', ':' ); $string = str_replace( $punc, '', $string ); # Split at whitespace, ignore empty returns $words = preg_split( '/\\s/', $string, -1, PREG_SPLIT_NO_EMPTY ); # Remove duplicates $words = array_unique( $words ); print_r( $words ); ?> Link to comment https://forums.phpfreaks.com/topic/117553-help-with-looping/#findComment-604610 Share on other sites More sharing options...
spires Posted July 31, 2008 Author Share Posted July 31, 2008 Perfect :-) That worked great Could you please explain why? Thanks Link to comment https://forums.phpfreaks.com/topic/117553-help-with-looping/#findComment-604611 Share on other sites More sharing options...
discomatt Posted July 31, 2008 Share Posted July 31, 2008 It would help if you told us who you were talking to. Link to comment https://forums.phpfreaks.com/topic/117553-help-with-looping/#findComment-604613 Share on other sites More sharing options...
spires Posted July 31, 2008 Author Share Posted July 31, 2008 I tried ainoy31 method Thanks for your help though Link to comment https://forums.phpfreaks.com/topic/117553-help-with-looping/#findComment-604616 Share on other sites More sharing options...
ainoy31 Posted July 31, 2008 Share Posted July 31, 2008 array_unique() sorts the values and treat them as a string. Checks and then will keep the first key encountered for every value and ignore all following keys. Link to comment https://forums.phpfreaks.com/topic/117553-help-with-looping/#findComment-604637 Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 You could also easily use strtok() for something like this: <?php $str = <<<EOS one two three one EOS; $words = array(); $tok = strtok($string, "\n"); while ($tok !== false) { if (!in_array($tok, $words)) { $words[] = $tok; } $tok = strtok("\n\t"); } echo "<pre>" . print_r($words, TRUE) . "</pre>"; ?> Link to comment https://forums.phpfreaks.com/topic/117553-help-with-looping/#findComment-604639 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.