php - String Replace Alphabet Ignoring HTML Tags and Elements -


this code want to. issue replaces html tags , elements. want code ignore html tags , elements, , replace letters aren't within <...> of tags.

currently, replaces every letter upper , lower letter. , prevents replaced letters being replaced again. splendid. if remove tags , leave plain text, would, it's not desired result.

this needs work simple or complex strings. merely simple version of being done for.

tags included <p><br><strong><b><em><i><u><strike><s>  $foo = ' <p> <i>foobar</i> walks down street. <br /> <br /> <b style="font-size: 10px;">foo!</b> <br /> <br /> <strike>foobar.</strike> </p> <p> <i>foobar</i> walks down street. <br /> <br /> <b style="font-size: 10px;">foo!</b> <br /> <br /> <strike>foobar.</strike> </p> ';  $replaces = array( 'a' => 'q', 'b' => 'f', 'c' => 'j', 'd' => 't', 'e' => 'c', 'f' => 'w', 'g' => 'n', 'h' => 'y', 'i' => 'l', 'j' => 'h', 'k' => 's', 'l' => 'v', 'm' => 'z', 'n' => 'x', 'o' => 'u', 'p' => 'r', 'q' => 'm', 'r' => 'p', 's' => 'o', 't' => 'k', 'u' => 'i', 'v' => 'g', 'w' => 'a', 'x' => 'b', 'y' => 'd', 'z' => 'e', 'a' => 'f', 'b' => 'o', 'c' => 't', 'd' => 'k', 'e' => 'i', 'f' => 'd', 'g' => 'u', 'h' => 'r', 'i' => 'p', 'j' => 'x', 'k' => 'z', 'l' => 'q', 'm' => 's', 'n' => 'v', 'o' => 'w', 'p' => 'y', 'q' => 'n', 'r' => 'l', 's' => 'm', 't' => 'j', 'u' => 'a', 'v' => 'g', 'w' => 'e', 'x' => 'b', 'y' => 'c', 'z' => 'h', );  for( $i=0,$l=strlen($foo_replace);$i<$l;$i++ ){     if( isset($replaces[$foo_replace[$i]]) ){         $foo_replace[$i] = $replaces[$foo_replace[$i]];     } } 

i've been googling how replace , ignore html tags, none of results provided me solid, nor exemplifying. thing seemed right answer html dom parser... however, can't find decent examples particular issue. therefore, wasn't useful be. if provide me explanation or example solve problem, great.

edit

using answer given, tried figure out meant. wasn't given link parser being used, had google around find it. using code removed html , changed smart tags strange characters... not wanted. kept looking ways using php dom class, , still can't figure out. don't see how put html tags in place after making changes need do.

this ended in trying so...

function encodeme($str , $replaces){     for( $i=0;$i<strlen($str);$i++ ){         if(isset($replaces[$str[$i]]) ){         $str[$i] = $replaces[$str[$i]];     } } return $str; }  $dom = new domdocument; $dom->loadhtml($chapter_replace); $nodes = $dom->childnodes; foreach($nodes $node) {  for($i=0,$l=strlen($node);$i<$l;$i++){     if(isset($replaces[$node[$i]])){         $node[$i] = $replaces[$node[$i]];     } }  } $chapter_replace = $dom->savehtml(); 

this should started...

  $str = 'the red hen walks down street. <b style="font-size: 10px;">bang!</b> end.';    $dom = new domdocument;   $dom->loadhtml($str);    // encoding functin   function encodeme($str , $replaces){     for( $i=0;$i<strlen($str);$i++ ){         if( isset($replaces[$str[$i]]) ){          $str[$i] = $replaces[$str[$i]];         }      }      return $str;    }     // loop through dom, , build encoded string    // here can figure out way attach html tag front    // retain tags uncoded.  didn't take time that.    // should enough started though        $newstring="";        foreach ($dom->childnodes $node)         {           $newstring.=encodeme($node->nodevalue, $replaces);            // pseudocode of how might it...           // $newstring.= attach front tag. encodeme($node->nodevalue, $replaces). attach tag;         };    echo $newstring; 

it along these lines, not quite. you'll have play around abit...but started

edit

heres hackish way it. without domdocument....but works.

      $str = '<span class="test">the red hen walks down street.</span> <span class="second">bang!</span> end.';       $html=array();       //explode html string, < tag       $firstex = explode("<",$str);        $i=0;       // loop through array, , explode > tag, encode text portion       // , store pair in $html array       foreach($firstex $t){          $i++;          $tmp = explode(">",$t);          $html[$i]['tag'] = $tmp[0];          if(isset($tmp[1])){            $html[$i]['inner']=encodeme($tmp[1],$replaces);          }       }           //loop through pair array, , build new string          $newstring="";          foreach($html $h){           if(!empty($h['tag'])){             $newstring.="<".$h['tag'].">";           }           if(!empty($h['inner'])){              $newstring.=$h['inner'];           }          }          // viola!         echo $newstring;           // string encoding function         function encodeme($str , $replaces){          for( $i=0;$i<strlen($str);$i++ ){            if( isset($replaces[$str[$i]]) ){             $str[$i] = $replaces[$str[$i]];            }          }          return $str;         } 

example here example here


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -