这段代码几乎完全符合我的要求。唯一的问题是它还替换了HTML标记和元素。我想让这段代码忽略那些HTML标签和元素,只替换不在<…
目前,它将每个字母UPPER和LOWER替换为另一个字母。它还可以防止已经被替换的字母再次被替换。这太棒了。如果我可以删除标记并将其保留为纯文本,我会这样做,但这不是期望的结果。
这需要为简单或复杂的字符串工作。这只是一个简单的版本,这是做什么。
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解析器…然而,我找不到任何合适的例子来说明这个问题。因此,它并没有发挥出应有的作用。如果有人能给我一个解释或例子来解决这个问题,那就太好了。
编辑
根据给出的答案,我试图弄清楚这一切是什么意思。我没有得到正在使用的解析器的链接,所以我不得不在谷歌上找到它。使用该代码删除了所有的HTML,并将智能标签更改为奇怪的字符…这不是我想要的。因此,我一直在寻找使用PHP DOM类来实现这一点的方法,但我仍然无法找到方法。我不知道如何在做了需要做的更改后将HTML标签放回原位。这就是我试图这样做的结果…
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();
这应该可以让你开始…
$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;
它将是类似的东西,但不完全是。你肯定得玩一玩……但这只是一个开始
编辑
这里有一个简单的方法。没有DOMDocument……
$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;
}
例子例子