Link to home
Start Free TrialLog in
Avatar of Steve Tinsley
Steve TinsleyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

php code error - help

I am using some code to insert a string into a database but split it up using . or , or spaces etc
BUT I am getting an error.

Could anyone help?

$string=$answer;
					
	// get the input string from the post and then tokenize it to get each word, save the words in an array  in case the word is repeated add '1' to the existing words counter
	$count=0;
	$tok = strtok($string, " \t,;.\'\"!&-`\n\r");//considering line-return,line-feed,white space,comma,ampersand,tab,etc... as word separator
	
	if(strlen($tok)>0) $tok=strtolower($tok);
	$words=array();
	$words[$tok]=1;
	while ($tok !== false) {
		//echo "Word=$tok<br />";
		$tok = strtok(" \t,;.\'\"!&-`\n\r");
		if(strlen($tok)>0) {
			$tok=strtolower($tok);
			if($words[$tok]>=1){
				$words[$tok]=$words[$tok] + 1;
			} else {
				$words[$tok]=1;
			}
		}
	}

Open in new window


I am testing inserting aaa.bbb.

Error:
<b>Notice</b>:  Undefined index: bbb in <b>/Users/steve/Sites/stinteractive/v2.9/touch/library/votingProcess.php</b> on line <b>256</b><br />


Any ideas??

Many Thanks

Steve
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Well, obviously the array is not formed as expe the or your code is wrong, but it's difficult to say without seeing the code. Please post it here.
try this:

$string="Now is the time for all good men to come to the aid of their country";
	$words=array();
					
	// get the input string from the post and then tokenize it to get each word, save the words in an array  in case the word is repeated add '1' to the existing words counter
	$tok = strtok($string, " \t,;.\'\"!&-`\n\r");//considering line-return,line-feed,white space,comma,ampersand,tab,etc... as word separator
	
	if($tok) $words[$tok]=1;
	while ($tok !== false) {
		//echo "Word=$tok<br />";
		$tok = strtok(" \t,;.\'\"!&-`\n\r");
			if(isset($words[$tok])){
				$words[$tok]++;
			} else {
				$words[$tok]=1;
			}
		}
var_dump($words);

Open in new window

Avatar of Steve Tinsley

ASKER

Hi,

Ive tried the suggested code and it works BUT I get a 0 at the end:

array(14) {
  ["is"]=>
  int(1)
  ["the"]=>
  int(2)
  ["time"]=>
  int(1)
  ["for"]=>
  int(1)
  ["all"]=>
  int(1)
  ["good"]=>
  int(1)
  ["men"]=>
  int(1)
  ["to"]=>
  int(2)
  ["come"]=>
  int(1)
  ["aid"]=>
  int(1)
  ["of"]=>
  int(1)
  ["their"]=>
  int(1)
  ["country"]=>
  int(1)
  [0]=>
  int(1)
}


What's the best way to loose this??
ASKER CERTIFIED SOLUTION
Avatar of mankowitz
mankowitz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial