Adding to an empty array if word is found in two set of texts

I’m trying to figure out how to add words to an empty array. The words that are found in 2 texts should add to one array and those not found should be added in another array. However if there are words found and their synonymous or other words related are not found I still want them to be added to the array of found keywords.

// $kw grabs the group of keywords from the url:
// keywords=pride,proud;kingdom;stablish; ever;throne;judgment;head;tail;peace;born;child;Philistines;David;heart;cut off;devour;destr;speak,spoke;judg;Jacob;Israel;brother;blood;stretch;hand
$kw = $_GET["keywords"];

//breaks down in an array
$keywords = explode(";", $kw);
$keyword_color = array();
$kwNotFound = array();

// these are the 2 texts
for($j=0; $j < count($id); $j++){
    $text = $text.stripslashes(mysql_escape_string($textData[$j]));
}
for($j=0; $j < count($id2); $j++){
    $text2 = $text2.stripslashes(mysql_escape_string($textData2[$j]));
}


for($i=0; $i < count($keywords);$i++){
    $keywordArr[$i] = explode(",", $keywords[$i]);
    for($c=0; $c < count($keywordArr[i]);$c++){
        if ((preg_match("/".$keywordArr[i][$c]."/i", $text)) && (preg_match("/".$keywordArr[i][$c]."/i", $text2))) {
            // keywords found should be added to this array (although if "pride" is found and "proud" isn't found then proud is added to the next array below in the else - which is not what I wanted. I want "pride" and "proud" to be added in this array even if 1 of the 2 is found in both texts)
                        // "pride" and "proud" had a comma (,) in the url whereas words not related have a semicolon (;) in the url
            array_push($keyword_color, array($keywordArr[i][$c], $colors[$i]));
        } else {
            // keywords not found should be added to this array
            array_push($kwNotFound, array($keywordArr[i][$c], $colors[$i]));
        }
    }
}
1 Like

I am confused about the words and synonyms. Could you rephrase the explanation?

I have made a start regarding words in one array and not in the other"

<?php //=========================================================================

// DEBUG
  function fred($val, $title)
  {
    echo '<pre>';
      echo $title ? $title .' => ' :NULL;
      print_r($val);
    echo '</pre>';
  }

echo $rqd = <<< TMP
/*
<pre>
  I'm trying to figure out how to add words to an empty array. 

  The words that are found in both texts should add to "in[]"
   and those not found should be added in "out[]". 

   However if there are words found and their synonymous 
   or other words related are not found 
   I still want them to be added to the array of found keyWords.
*/
TMP;

// Source text
  if(0):
    $kw = $_GET["keyWords"];
  else:
    $txt_1 = 'pride,proud;kingdom;stablish; ever;throne;judgment;head;tail;peace;born;child;Philistines;'
           . 'David;heart;cut off;devour;destr;speak,spoke;judg;Jacob;Israel;brother;blood;stretch;hand';

    $txt_2 = 'pride,proud;kingdom;stablish; ever;throne;judgment;head;tail;peace;born;child;Philistines';
              // David;heart;cut off;devour;destr;speak,spoke;judg;Jacob;Israel;brother;blood;stretch;hand';
  endif;

//breaks down in an array
  $keyWords = explode(";", $txt_1);
  $keyOther = explode(";", $txt_2);

// arrays to fill
  $in  = [];
  $out = [];

// step through #keyWords
  foreach( $keyWords as $keyWord ):
    if( in_array($keyWord, $keyOther) ):
      $in[]  = $keyWord;
    else:
      $out[] = $keyWord;
    endif;  
  endforeach;

// Results
  fred($txt_1, '$text_1');
  fred($txt_2, '$text_2');

  fred($in,  '$in');
  fred($out, '$out');

Output:

this is from the url:
&keywords=pride,proud;kingdom;stablish; ever;throne;judgment;head;tail;peace;born;child;Philistines;David;heart;cut off;devour;destr;speak,spoke;judg;Jacob;Israel;brother;blood;stretch;hand

The ones separated by a semicolon are different words, whereas the ones separated by a comma are either synonymous words or words that belong together like “pride,proud”.

I forgot to mention at the beginning that $colors[$i] is actually an array of colors to be used to highlight the words in the text, if they are found. And written in the array of the words NOT found separately.

I had figured out part of this before. But when I tried to look more into this, things got messed up further.

Ok this is what I had (see code below) but if “pride” was found but “proud” wasn’t found “proud” was set aside in the array of words not found in common between 2 texts, which is not what I want. I want “proud” to appear in the array of words found because “pride” is there as well. I hope you understand what I mean.

<?php

$keywords = explode(";", $kw);
$keyword_color = array();
$kwNotFound = array();
for($j=0; $j < count($id); $j++){
    $text = $text.stripslashes(mysql_escape_string($textData[$j]));
}
for($j=0; $j < count($id2); $j++){
    $text2 = $text2.stripslashes(mysql_escape_string($textData2[$j]));
}
for($i=0; $i < count($keywords);$i++){
    $keywordArr = explode(",", $keywords[$i]);
    //if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
        for($c=0; $c < count($keywordArr);$c++){
            if ((preg_match("/".$keywordArr[$c]."/i", $text)) && (preg_match("/".$keywordArr[$c]."/i", $text2))) {
                //echo "A match was found.<br />\n";
                //echo $keywordArr[$c]." = <br />\n";
                array_push($keyword_color, array($keywordArr[$c], $COLORS[$i]));
            } else {
                //echo $keywordArr[$c]." = not found.<br />\n";
                array_push($kwNotFound, array($keywordArr[$c], $COLORS[$i]));
                //$kwNotFound[] = $keywordArr[$c];
            }
    }
}

?>
<p class="q">These words are found in common:<br />
<?php 
    for($j=0; $j < count($keyword_color);$j++){
        
        if($j > count($keyword_color)-2){
            echo " and \n";
        }
        echo "<span style=\"font-weight: bold; color: ".$keyword_color[$j][1]."\">".$keyword_color[$j][0]."</span>";
        if($j < count($keyword_color)-2){
            echo ", \n";
        }

    }
    echo ". \n";
?>
</p>
<br />
<p class="q">These words are not found in common:<br />
<?php 
    for($j=0; $j < count($kwNotFound);$j++){
        
        if($j > count($kwNotFound)-2){
            echo " and \n";
        }
        echo "<span style=\"font-weight: bold; color: ".$kwNotFound[$j][1]."\">".$kwNotFound[$j][0]."</span>";
        if($j < count($kwNotFound)-2){
            echo ", \n";
        }

    }
echo ". \n";    
?>

</p>

I am still confused :frowning:

There are only a few words involved, please supply the results that you want in all of the arrays.

Also exactly what is required with the missing colours.

These are the list of colors in an array (do you know any better list of variety of colors for highlighting words in a text?):

$colors = Array(
"red",
"blue",
"green",
"orange",
"purple",
"sienna",
"darkolivegreen",
"darkgreen",
"darkslateblue",
"navy",
"indigo",
"darkslategray",
"darkred",
"darkorange",
"olive",
"teal",
"slategray",
"dimgray",
"sandybrown",
"yellowgreen",
"seagreen",
"mediumturquoise",
"pink",
"gray",
"magenta",
"lime",
"cyan",
"deepskyblue",
"darkorchid",
"silver",
"paleturquoise",
"wheat",
"palegreen",
"lightblue",
"plum",
"black",
"#741b47",
"#351c75",
"#0b5394",
"#134f5c",
"#38761d",
"#bf9000",
"#b45f06",
"#990000",
"#a64d79",
"#674ea7",
"#3d85c6",
"#45818e",
"#6aa84f",
"#e69138",
"#cc0000",
"#c27ba0",
"#8e7cc3",
"#6fa8dc",
"#76a5af",
"#93c47d",
"#f6b26b",
"#e06666",
"#d5a6bd",
"#b4a7d6",
"#9fc5e8",
"#a2c4c9",
"#b6d7a8",
"#f9cb9c",
"#ea9999"
);

Ok this is what I’m trying to do (my blog):
http://gilgalsbiblewheel.blogspot.ca/2016/02/isaiah-3.html

I think this is what your want.

It works and is rough and ready so I will leave it up to you to optimise:

Online Demo

Parse error: syntax error, unexpected ‘[’ in …\keywords-test.php on line 69:

    $aClrs  = [
    "red","blue","green","orange","purple","sienna","darkolivegreen","darkgreen","darkslateblue","navy","indigo","darkslategray","darkred","darkorange","olive","teal","slategray","dimgray","sandybrown","yellowgreen","seagreen","mediumturquoise","pink","gray","magenta","lime","cyan","deepskyblue","darkorchid","silver","paleturquoise","wheat","palegreen","lightblue","plum","black","#741b47","#351c75","#0b5394","#134f5c","#38761d","#bf9000","#b45f06","#990000","#a64d79","#674ea7","#3d85c6","#45818e","#6aa84f","#e69138","#cc0000","#c27ba0","#8e7cc3","#6fa8dc","#76a5af","#93c47d","#f6b26b","#e06666","#d5a6bd","#b4a7d6","#9fc5e8","#a2c4c9","#b6d7a8","#f9cb9c","#ea9999"
     ];

The Online Demo passes the W3.org validation test so it looks as though your copy and paste did not work.

https://validator.w3.org/nu/?doc=http%3A%2F%2Fwww.johns-jokes.com%2Fdownloads%2Fsp-f%2Fgilgalbiblewheel%2F

It could just be a really old version of PHP that doesn’t understand that syntax.

1 Like

i’m working on my personal server

I have just updated the source code and it should be compatible with all PHP versions:

Online Demo

Now renders OK on mobiles and search and replace is now case-insensitive.

Parse error: syntax error, unexpected ‘[’ in C:\xampp.…\keywords-test.php on line 36

Yes, your set-up is likely too old a version of PHP
Try

 $aClrs  = array("red","blue","green","orange","purple","sienna","darkolivegreen","darkgreen","darkslateblue","navy"
,"indigo","darkslategray","darkred","darkorange","olive","teal","slategray","dimgray","sandybrown","yellowgreen"
,"seagreen","mediumturquoise","pink","gray","magenta","lime","cyan","deepskyblue","darkorchid","silver","paleturquoise"
,"wheat","palegreen","lightblue","plum","black","#741b47","#351c75","#0b5394","#134f5c","#38761d","#bf9000"
,"#b45f06","#990000","#a64d79","#674ea7","#3d85c6","#45818e","#6aa84f","#e69138","#cc0000","#c27ba0"
,"#8e7cc3","#6fa8dc","#76a5af","#93c47d","#f6b26b","#e06666","#d5a6bd","#b4a7d6","#9fc5e8","#a2c4c9"
,"#b6d7a8","#f9cb9c","#ea9999");
1 Like

This is the next error:
$aWordsToFind=;

Parse error: syntax error, unexpected ‘[’ in C:\xampp.…\keywords-test.php on line 65

So if you are using a long dead version of PHP that expects array() instead of then either replace all the occurrences instead of simply returning here to ask about each one or get a not so long dead version of PHP that does support it (or perhaps even upgrade to a current version 5.6 or 7).

1 Like

I have just downgraded the script and it should now be OK for your outdated PHP version.

Let me know if you have any more problems.

EDIT
Can you let us know what PHP Version you are using?

<?php 
  echo phpversion(); 
?>

XAMPP for Windows Version 1.6.3a

i’m looking in upgrading it

1 Like