Solved

php code error - help

Posted on 2015-01-11
4
84 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 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 48
Blocking submission of a web-based form 6 20
PHP AJAX JSON 2 58
Decrypt string by php 7 29
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
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 …

786 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