jsims281forum Posted December 15, 2006 Share Posted December 15, 2006 My problem is I have two arrays which are exploded from strings, and I get screwy results when I try to compare them and find the common words. I think this is because it's counting some characters like whitespace and newline as part of the word...? At least thats what I *think* the problem is! I also get silly results when trying to find whole lines of text that the two have in common. I'm not sure, 'cause this is my first project ever in PHP, and the first thing I've had to program for ages so I'm more than a bit rusty!Here's my code in full, sorry about the mess! (it's online at http://44roughcuts.com/SSWL if you want to see it in operation)[code]<?phpif($_POST["text1"]== $_POST["text2"]) {print "These two texts are identical.<br><br>";}else {print "These two texts are different.<br><br>";}$text1string = $_POST["text1"];$text2string = $_POST["text2"];$wordArray = array(); for ($i = 0; $i <strlen($text1string);$i++) { $temp = $text1string; $char = $temp[$i]; $wordArray[$i] = $char; }$wordArray2 = array(); for ($j = 0; $j <strlen($text2string);$j++) { $temp2 = $text2string; $char2 = $temp2[$j]; $wordArray2[$j] = $char2; }//Reads the two strings into respective arrays//-------------------------------------------------------------------$linesoftext1 = split("\n",$text1string );//echo "Lines of text (1)= <br>";//for ($jjj=0;$jjj<count($linesoftext1);$jjj++)//{echo "$linesoftext1[$jjj]<br>";}//--------------------------------------------------------------------$linesoftext2 = split("\n",$text2string );//echo "Lines of text (2)= <br>";//for ($jjjj=0;$jjjj<count($linesoftext2);$jjjj++)//{echo "$linesoftext2[$jjjj]<br>";}//--------------------------------------------------------------------//echo "<br>";$comlinecount=0;for ($i=0;$i<(count($linesoftext2));$i++){// for ($j=0;$j<(count($linesoftext1));$j++)// { //if ($linesoftext2[$i]==$linesoftext1[$j]) if (in_array($linesoftext2[$i], $linesoftext1)) { echo "Common Line = $linesoftext1[$i]<BR>"; //echo "This was line $j in the first text and line $i in the second text.<br><br>"; $comlinecount++; }// }}//$os = array("Mac", "NT", "Irix", "Linux");//if (in_array("Irix", $os)) {// echo "Got Irix";//}//if (in_array("mac", $os)) {// echo "Got mac";//}$linescount1=count($linesoftext1);$linescount2=count($linesoftext2);$percentshared1 = ($comlinecount/$linescount1)*100;$percentshared2 = ($comlinecount/$linescount2)*100;echo "Text one: $linescount1 lines, Text two: $linescount2 lines.<br>";echo "_______________________________________________________<br>";echo "Text one has $percentshared1 per cent common lines with text two.<br>";echo "Text two has $percentshared1 per cent common lines with text one.<br><br>";//echo "<br> From the array:<br>";for ($k = 0;$k<strlen($text1string);$k++) { //echo "$wordArray[$k]"; }//echo "<BR>";for ($l = 0;$l<strlen($text2string);$l++) { //echo "$wordArray2[$l]"; }// echo "<BR><BR>";//Echos the two strings from their arraysif (strlen($text1string) > strlen($text2string)) { $maxstrlen=strlen($text1string); }else $maxstrlen=strlen($text2string);//Finds the maximum string length$wordcount = str_word_count( $text1string );$wordcount2 = str_word_count( $text2string );echo "Text 1 word count = $wordcount<br>";echo "Text 2 word count = $wordcount2<br>";//Echos the word count for each text$words=array(NULL);$number0 = 5;$y = 0; while ($y < $number0) { $words[$y] = $wordArray[$y]; ++$y; }IF ($wordcount>$wordcount2) {$maxwordcount=$wordcount;}ELSE $maxwordcount=$wordcount2;echo "Max word count: $maxwordcount<br>";$text1low = strtolower($text1string);$text2low = strtolower($text2string);$arrayofwords=str_word_count($text1low, 1);$arrayofwords2=str_word_count($text2low, 1);echo "_______________________________________________________<br>";$word_array[1] = explode(' ',$text1string);$word_array[2] = explode(' ',$text2string);sort($word_array[1]);sort($word_array[2]);$shared = array_intersect($word_array[1], $word_array[2]);for ($i=1; $i<3; $i++) { foreach ($word_array[$i] as $az) { if (in_array($az, $shared)) $out[$az][$i]++; }}echo "Words these texts have in common:<br>";if (count($shared)>0){echo '<table border="2"> <tr> <td><b>Word</b></td> <td><b>Count in Tx1</b></td> <td><b>Count in Tx2</b></td> </tr>';foreach ($out as $w => $b) echo " <tr> <td>$w</td> <td>$b[1]</td> <td>$b[2]</td> </tr>";echo '</table>';}asort($shared);for ($a = 0;$a<count($shared);$a++) { // echo "<BR>$shared[$a]"; }echo "<BR>";//Prints out the common words?><html></html>[/code]Any suggestions? Link to comment https://forums.phpfreaks.com/topic/30805-help-how-do-i-strip-whitespace-etc-from-an-array/ Share on other sites More sharing options...
complex05 Posted December 15, 2006 Share Posted December 15, 2006 str_replace("\n","",$result);str_replace("\t","",$result);etc etc etc Link to comment https://forums.phpfreaks.com/topic/30805-help-how-do-i-strip-whitespace-etc-from-an-array/#findComment-142023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.