<?php
// fillin_gaps_with_emojis.php
// RJM Programming
// June, 2021

 $objphp='';
 $emlist='';
 $intext='';

 if (file_exists('./fillin_gaps_with_emojis.tt')) {
   $objphp=file_get_contents('./fillin_gaps_with_emojis.tt');
 } else if (file_exists('./fillin_gaps_with_emojis.txt')) {
   $objphp=file_get_contents('./fillin_gaps_with_emojis.txt');
 }
 if (isset($_POST['intext']) || isset($_GET['intext'])) {
 if (isset($_POST['intext'])) {
   $intext=' ' . str_replace('+',' ',urldecode($_POST['intext'])) . ' ';
 } else {
   $intext=' ' . str_replace('+',' ',urldecode($_GET['intext'])) . ' ';
 }
 $enda="</a>";
 $ea=">";
 $za="<";
 $wastext=$intext;
 if (strpos($objphp, $enda) === false && strpos($objphp, ':') !== false) {
   $enda='~';
   $endspan='|';
   $ea="~";
   $za="|";
   $emarrpre=explode($ea, $objphp);
   $objphp=$ea;
   $emarr=[];
   $emarrlens=[];
   for ($ib=0; $ib<sizeof($emarrpre); $ib++) {
     array_push($emarrlens, ('' . (99 - strlen($emarrpre[$ib]))) . $emarrpre[$ib]);
   }
   sort($emarrlens);
   for ($ib=0; $ib<sizeof($emarrlens); $ib++) {
     $objphp.=substr($emarrlens[$ib], 2) . '~'; //array_push($emarr, substr($emarrlens[$ib], 2));
   }
   $emarr=explode($endspan, $objphp);
 } else {
   $emarr=explode($endspan, $objphp);
 }
 for ($ib=1; $ib<sizeof($emarr); $ib++) {
   $tonexta=explode($enda, $emarr[$ib])[0];
   $fromprevspan=explode($ea, $emarr[-1 + $ib])[-1 + sizeof(explode($ea, $emarr[-1 + $ib]))];
   if (strpos($tonexta, $za) === false && strpos($fromprevspan, ' ') === false) {
     //$emlist.=$tonexta . ':' . $fromprevspan . ';';
     $emlist.=$fromprevspan . '|' . $tonexta . '~';
     if (stripos($intext, $tonexta) !== false) {
       if ($tonexta != strtolower($tonexta)) {
         $thingos=explode(strtolower($tonexta), strtolower($intext));
         $lb=sizeof($thingos);
         for ($kb=1; $kb<$lb; $kb++) {
          if ($kb == 1) {
           array_push($thingos, ' ');
          }
          if ((substr(str_replace('/','a',(' ' . $thingos[-1 + $kb])), -1) < 'a' || substr(str_replace('/','a',(' ' . $thingos[-1 + $kb])), -1) > 'z') && (substr(($thingos[0 + $kb] . ' '), 0, 1) < 'a' || substr(($thingos[0 + $kb] . ' '), 0, 1) > 'z')) {
           $pref=substr((' ' . $thingos[-1 + $kb]), -1);
           $suff=substr(($thingos[0 + $kb] . ' '), 0, 1);
           //echo "pref=" . $pref . ' and tonexta=' . $tonexta . ' and thingos[-1 + kb]=' . $thingos[-1 + $kb] . '?';
           //exit;
           //$intext=str_replace($tonexta, strtolower($tonexta), $intext);
           //$intext=str_replace(strtolower($tonexta), '<SPAN TITLE="' . str_replace(" ","_",str_replace('"','',str_replace("'","",strtoupper($tonexta)))) . '">' . $fromprevspan . '</SPAN>', $intext);
           $intext=str_replace(($pref . $tonexta . $suff), strtolower(($pref . $tonexta . $suff)), $intext);
           $intext=str_replace(strtolower($pref . $tonexta . $suff), $pref . '<SPAN TITLE="' . str_replace(" ","$@!",str_replace('"','',str_replace("'","",strtoupper($tonexta)))) . '">' . $fromprevspan . '</SPAN>' . $suff, $intext);
          }
         }
       } else {
         $intext=str_replace($tonexta, '<SPAN TITLE="' . str_replace(" ","$@!",str_replace('"','',str_replace("'","",strtolower($tonexta)))) . '">' . $fromprevspan . '</SPAN>', $intext);
       }
     }
   }
 }
 if (!file_exists('./fillin_gaps_with_emojis.tt') && $emlist != '') {
   file_put_contents('./fillin_gaps_with_emojis.tt', '~' . $emlist);
 }
 }
 if ($intext != '') {
   echo "<html><head><title>Emoji-ize Text - RJM Programming - June, 2021</title><meta charset='UTF-8'></head><body style='width:100%;'><p>" . str_replace("\n","<br>",str_replace("$@!"," ",trim($intext))) . "</p></body></html>";
 }
 //echo "\n\n" . $emlist;

 exit;
?>
