Solved

php code error - help

Posted on 2015-01-11
4
81 Views
Last Modified: 2015-01-11
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
0
Comment
Question by:sjtinsley83
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 40543132
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.
0
 
LVL 24

Expert Comment

by:mankowitz
ID: 40543143
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

0
 

Author Comment

by:sjtinsley83
ID: 40543182
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??
0
 
LVL 24

Accepted Solution

by:
mankowitz earned 500 total points
ID: 40543187
Sure. Just move the test inside the loop..

$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
	
	while ($tok !== false) {
	if($tok) $words[$tok]=1;

			if(isset($words[$tok])){
				$words[$tok]++;
			} else {
				$words[$tok]=1;
			}
				$tok = strtok(" \t,;.\'\"!&-`\n\r");
		}
var_dump($words);

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now