Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php code error - help

Posted on 2015-01-11
4
Medium Priority
?
89 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

722 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