首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >字符串替换Alphabet忽略HTML标记和元素

字符串替换Alphabet忽略HTML标记和元素
EN

Stack Overflow用户
提问于 2014-06-26 01:15:49
回答 1查看 294关注 0票数 0

这段代码几乎完全符合我的要求。唯一的问题是它还替换了HTML标记和元素。我想让这段代码忽略那些HTML标记和元素,只替换不在标签的<...>中的字母。

目前,它用另一个字母替换每个字母的上下。它还可以防止已经替换的字母再次被替换。这真是太棒了。如果我可以删除标记并将其保留为纯文本,我会这样做,但这并不是期望的结果。

这需要用于简单或复杂的字符串。这仅仅是这件事的一个简单的版本。

代码语言:javascript
复制
Tags that could be included <p><br><strong><b><em><i><u><strike><s>

$foo = '
<p>
The <i>foobar</i> walks down the street.
<br />
<br />
<b style="font-size: 10px;">Foo!</b>
<br />
<br />
<strike>Foobar.</strike>
</p>
<p>
The <i>foobar</i> walks down the 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]];
    }
}

我一直在谷歌上搜索如何替换和忽略html标签,但没有一个结果为我提供了可靠的东西,也没有举例说明。似乎唯一正确的答案是HTML DOM Parser..。然而,我找不到任何合适的例子来解决这个问题。因此,它并没有那么有用。如果有人能给我一个能解决这个问题的解释或例子,那就太好了。

编辑

利用给出的答案,我试图弄清楚这一切意味着什么。我没有得到一个解析器使用的链接,所以我不得不在谷歌上找到它。使用该代码将删除所有HTML并将智能标记更改为奇怪的字符.不是我想要的。因此,我一直在寻找使用PHP类来实现这一目标的方法,但我仍然无法解决这个问题。我不知道如何在做完我需要做的更改之后,将HTML标记放回原处。

这就是我最后试图这么做的原因.

代码语言:javascript
复制
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 as $node) {

for($i=0,$l=strlen($node);$i<$l;$i++){
    if(isset($replaces[$node[$i]])){
        $node[$i] = $replaces[$node[$i]];
    }
}

}
$chapter_replace = $dom->saveHTML();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-26 01:54:27

这应该让你开始..。

代码语言:javascript
复制
  $str = 'The red hen walks down the street. <b style="font-size: 10px;">Bang!</b> The end.';

  $DOM = new DOMDocument;
  $DOM->loadHTML($str);

  // The 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 the DOM, and build an encoded string
   // Here you can figure out a way to also attach the html tag to the front
   // To retain the tags uncoded.  But I didn't take the time to do that.
   // This should be enough to get you started though
       $newString="";
       foreach ($DOM->childNodes as $node)
        {
          $newString.=encodeMe($node->nodeValue, $replaces);

          // Pseudocode of how you might do it...
          // $newString.= attach front tag. encodeMe($node->nodeValue, $replaces). attach back tag;
        };

  echo $newString;

这将是一些类似的东西,但不完全是。你一定要和abit...but玩--这只是为了让你开始

编辑

这是一种简单的方法。没有DOMDocument....But,它就能工作。

代码语言:javascript
复制
      $str = '<span class="test">The red hen walks down the street.</span> <span class="second">Bang!</span> The end.';
      $html=array();
      //Explode your html string, by the < tag
      $firstEx = explode("<",$str);

      $i=0;
      // Loop through that array, and explode the > tag, encode the text portion
      // and store the pair in the $html array
      foreach($firstEx as $t){
         $i++;
         $tmp = explode(">",$t);
         $html[$i]['tag'] = $tmp[0];
         if(isset($tmp[1])){
           $html[$i]['inner']=encodeMe($tmp[1],$replaces);
         }
      }

         //Loop through that pair array, and build the new string
         $newString="";
         foreach($html as $h){
          if(!empty($h['tag'])){
            $newString.="<".$h['tag'].">";
          }
          if(!empty($h['inner'])){
             $newString.=$h['inner'];
          }
         }

        // VIOLA!
        echo $newString;


        // The 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;
        }

这里的示例这里的例子

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24421026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档