0

I want to add html i tag to substring in a string to those matched to the element of the array. If the matched substring has i tag then don't add else add. here's my sample code.

   function itilizedWords($subjects){

  $arrayToSearch = array("Prima facie", "De facto", "Vis-à-vis", "De future",
      "De future", "De integro", "De integro", "De jure", 
      "Inter alia", "De novo", "Viz", "De minimis", "Res gestae",
     "De minimis non curat lex", "Res Gestae Divi Augusti", "et seq.",
     "et seq.", "Ex facie ", "A priori ", "A priori", "A posteriori",
     "Ex gratia", "A quo ", "Ex officio", "Ab extra", "Ab extra", "Ab 
      initio",
     "Ex post facto", "Absque hoc", "Factum", "Actori incumbit probation", 
     "Habeas corpus", 
      "Actus reus",  "Functus officio", "Ad coelom ", "Ad coelom ", "Ad 
      colligenda bona", 
      "Idem ", "Ad hoc", "In absentia", "Ad hominem", "In curia", "Ad idem", 
  "In extenso", 
     "Ad infinitum", "In futuro", "Ad litem", "In haec verba", "Ad quod damnum", "In limine",
      "Ad valorem","Pari material", "Adjournment sine die", "Adjournment sine die",
      "Amicus curiae", "Amicus curiae", "Animus nocendi", "Inter se", "Animus revertendi",
      "Intra vires", "Arguendo", "Arguendo", "Audi alteram partem", "Ipso facto", "Bona fide",
      "Jus cogens", "Mala fide", "Locus standi", "Certiorari", "Mandamus","Contra proferentem",
      "Contra proferentem","Coram non judice","Modus operandi",
      "Cuius est solum eius est usque ad coelum et ad inferos","Nemo judex in sua causa",
      "Status quo ante","Nisi","Stare decisis","Non est factum","Per se ","Per se ","Res gestae ",
      "Uberrima fides","Vice versa","Mutatis Mutandis","Wednesbury");

     $matchedwords=array();
    $offset = 0;
     $allpos = array();
    $pos=0;
    foreach ($arrayToSearch as $value) {
      while(($pos = strpos($subjects, $value, $offset))!== false){
       $offset   = $pos + 1;
        $allpos[] = $pos;
        $matchedwords[]= $value;
     if(strpos(substr($subjects, ($pos - 4), strlen($value)+ 6), '<i>')){
        return $subjects;
       } 
      else {
      return $replace = str_replace(substr($subjects, $pos, strlen($value)), '<i>'. substr($subjects, $pos, strlen($value)).'</i>', $subjects);
     $pos = $pos + 7;
       }

      }
    }

       }

       $subjects ="This is the Prima facie tried to do this it's been ....Prima facie <p><i>Prima facie</i></P> Prima facie  <p>Status quo ante  Uberrima fides</P> <i>Uberrima fides</i> <p>Mutatis Mutandis Wednesbury</p> <p><i>In futuro</i></p>";

         echo htmlentities(itilizedWords($subjects));

what's wrong with this code is that it does add i tag for all the matched words and make some substrings have double i tag. So, I want to avoid. Please any help is welcomed and thank you in advance.

Mamadou Barry
  • 297
  • 2
  • 7
  • What is the expected output I have a hard time understanding the code – Andreas Jun 13 '18 at 05:19
  • the expected out is This is the Prima facie tried to do this it's been ....Prima facie

    Prima facie

    Prima facie

    Status quo ante Uberrima fides

    Uberrima fides

    Mutatis Mutandis Wednesbury

    In futuro

    – Mamadou Barry Jun 13 '18 at 05:51

2 Answers2

0

The main problem when replacing from a list are partial duplicates:

For example, you have "De minimis" and "De minimis non curat lex"

In a text like :

De minimis non curat lex

The result will be

<i><i>De minimis</i> non curat lex<i>

Also, you have already formatted tags inside the original text. So, you need to check this before replacing, to avoid duplicate replacing

Additionally, the code will only run only for the first word found, because you are using return inside the while. Consider using continue when no action is needed and return the result at the end, after the while.

Finally, using str_replace in the whole string always will be duplicating replacements when you call it repeatedly.

This is an updated code with some improvements:

<?php

function itilizedWords($subjects){

  $arrayToSearch = array("Prima facie", "De facto", "Vis-à-vis", "De future",
      "De future", "De integro", "De integro", "De jure", 
      "Inter alia", "De novo", "Viz", "De minimis", "Res gestae",
     "De minimis non curat lex", "Res Gestae Divi Augusti", "et seq.",
     "et seq.", "Ex facie ", "A priori ", "A priori", "A posteriori",
     "Ex gratia", "A quo ", "Ex officio", "Ab extra", "Ab extra", "Ab 
      initio",
     "Ex post facto", "Absque hoc", "Factum", "Actori incumbit probation", 
     "Habeas corpus", 
      "Actus reus",  "Functus officio", "Ad coelom ", "Ad coelom ", "Ad 
      colligenda bona", 
      "Idem ", "Ad hoc", "In absentia", "Ad hominem", "In curia", "Ad idem", 
  "In extenso", 
     "Ad infinitum", "In futuro", "Ad litem", "In haec verba", "Ad quod damnum", "In limine",
      "Ad valorem","Pari material", "Adjournment sine die", "Adjournment sine die",
      "Amicus curiae", "Amicus curiae", "Animus nocendi", "Inter se", "Animus revertendi",
      "Intra vires", "Arguendo", "Arguendo", "Audi alteram partem", "Ipso facto", "Bona fide",
      "Jus cogens", "Mala fide", "Locus standi", "Certiorari", "Mandamus","Contra proferentem",
      "Contra proferentem","Coram non judice","Modus operandi",
      "Cuius est solum eius est usque ad coelum et ad inferos","Nemo judex in sua causa",
      "Status quo ante","Nisi","Stare decisis","Non est factum","Per se ","Per se ","Res gestae ",
      "Uberrima fides","Vice versa","Mutatis Mutandis","Wednesbury");

     $matchedwords=array();
    $offset = 0;
     $allpos = array();
    $pos=0;
    foreach ($arrayToSearch as $value) {
      while(($pos = strpos($subjects, $value, $offset))!== false){
        $pos2=strpos($subjects, "<i>$value</i>", $offset) ;
        $offset   = $pos + 1;
          if ($pos2==$pos-3){
               // $value is yet between tags
               $offset   = $pos + 5;
              continue;
          }
        $prev=substr($subjects,0,$pos);
        $next=substr($subjects,$pos+strlen($value));  

       $subjects = $prev . '<i>'. $value.'</i>'.  $next;
       $offset +=7;

      }
    }
    return $subjects;

       }

       $subjects ="This is the Prima facie tried to do this it's been ....Prima facie <p><i>Prima facie</i></P> Prima facie  <p>Status quo ante  Uberrima fides</P> <i>Uberrima fides</i> <p>Mutatis Mutandis Wednesbury</p> <p><i>In futuro</i></p>";

         echo htmlentities(itilizedWords($subjects));
?>
F.Igor
  • 4,119
  • 1
  • 18
  • 26
0

I'd probably do it like this:

<?php

function itilizedWords( $subjects, $arrayToSearch ){

    foreach( $arrayToSearch as $chars )
    {
        $regex = '|(?!<i>)(' . $chars . ')(?!</i>)|';

        if( preg_match( $regex, $subjects, $matches) )
            $subjects = preg_replace( $regex, '<i>' . $matches[0] . '</i>', $subjects );
    }

    return $subjects;
}

$arrayToSearch = [
    "Prima facie", "De facto", "Vis-à-vis", "De future",
    "De future", "De integro", "De integro", "De jure", "Inter alia", "De novo", 
    "Viz", "De minimis", "Res gestae","De minimis non curat lex", 
    "Res Gestae Divi Augusti", "et seq.","et seq.", "Ex facie ", "A priori ", 
    "A priori", "A posteriori","Ex gratia", "A quo ", "Ex officio", "Ab extra", 
    "Ab extra", "Ab initio","Ex post facto", "Absque hoc", "Factum", 
    "Actori incumbit probation", "Habeas corpus", "Actus reus",  "Functus officio", 
    "Ad coelom ", "Ad coelom ", "Ad colligenda bona", "Idem ", "Ad hoc", "In absentia", 
    "Ad hominem", "In curia", "Ad idem", "In extenso", "Ad infinitum", "In futuro", 
    "Ad litem", "In haec verba", "Ad quod damnum", "In limine","Ad valorem",
    "Pari material", "Adjournment sine die", "Adjournment sine die","Amicus curiae", 
    "Amicus curiae", "Animus nocendi", "Inter se", "Animus revertendi","Intra vires", 
    "Arguendo", "Arguendo", "Audi alteram partem", "Ipso facto", "Bona fide",
    "Jus cogens", "Mala fide", "Locus standi", "Certiorari", "Mandamus",
    "Contra proferentem","Contra proferentem","Coram non judice","Modus operandi",
    "Cuius est solum eius est usque ad coelum et ad inferos","Nemo judex in sua causa",
    "Status quo ante","Nisi","Stare decisis","Non est factum","Per se ","Per se ",
    "Res gestae ","Uberrima fides","Vice versa","Mutatis Mutandis","Wednesbury"
];

$subjects ="This is the Prima facie tried to do this it's been ....Prima facie <p><i>Prima facie</i></P> Prima facie  <p>Status quo ante  Uberrima fides</P> <i>Uberrima fides</i> <p>Mutatis Mutandis Wednesbury</p> <p><i>In futuro</i></p>";

echo htmlentities( itilizedWords( $subjects, $arrayToSearch ) );
Brian Gottier
  • 4,522
  • 3
  • 21
  • 37