Search code examples
phpstr-replacestrpos

How to add html i tag to sub-strings that matched the elements of the array if they don't already have the tag


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.


Solution

  • 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 ) );