Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php code error - help

Posted on 2015-01-11
4
Medium Priority
?
92 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:Steve Tinsley
  • 2
4 Comments
 
LVL 31

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:Steve Tinsley
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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
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 count occurrences of each item in an array.
Suggested Courses

916 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