Jump to content

help with looping


spires

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.