learning_brain
New Member
- Messages
- 206
- Reaction score
- 1
- Points
- 0
I am tying to analyse a string in a number of way for my new adaptive learning language program at www.brain.x10hosting.com.
One part is to determine which words in the string are common, and thus remove them.
This test pages is at www.brain.x10hosting.com/test.php
The php below goes with a form - obviously.
$proc_input_array should only contain clean words not found in a db.
The trouble is that the ereg_replace changes all non-letter characters for spaces. Then when it comes to the explode, it adds another value to the array for each added space.....
How can I get rid of these extra spaces and stick with the words alone?
Also, words with characters in them get split, like "don't". This gets split into "don" and "t".
One part is to determine which words in the string are common, and thus remove them.
This test pages is at www.brain.x10hosting.com/test.php
The php below goes with a form - obviously.
PHP:
echo "Input: ".$_POST['input']."<br/>";
//clean input
$input = $_POST['input'];
$clean_input = ereg_replace("[^A-Za-z]", " ", $input);
echo "Cleaned input: ".$clean_input."<br/>";
//split response into words and count
$input_array = explode(" ", $clean_input);
$input_array_count = count($input_array);
echo stripslashes($input_array_count)." words.<br/><br/>";
//loop through each word and check if common.
for($i=0;$i<$input_array_count;$i++){
echo "Loop ".$i.": ";
$word_to_test = $input_array[$i];
echo "Word tested: (".$word_to_test."): ";
$sql_common="SELECT WORD FROM COMMONWORDS WHERE WORD = '".$word_to_test."' ";
$result_common = mysql_query($sql_common);
$row_common = mysql_fetch_array($result_common);
//if common
if (isset($row_common['WORD'])){
echo $row_common['WORD']." Found in Database.<br/><br/>";
}
//if not common
if (!isset($row_common['WORD'])){
echo " Is not common. <br/><br/>";
$proc_input_array[] = $word_to_test;
}
}
print_r($proc_input_array);
$proc_input_array should only contain clean words not found in a db.
The trouble is that the ereg_replace changes all non-letter characters for spaces. Then when it comes to the explode, it adds another value to the array for each added space.....
How can I get rid of these extra spaces and stick with the words alone?
Also, words with characters in them get split, like "don't". This gets split into "don" and "t".
Last edited: