将HTML文本转换为Leet(1337)与XPath对话

时间:2012-11-23 15:33:45

标签: php html xpath domdocument

我想将网页转换为leet(1337)与XPath和PHP对话。

只能用PHP完成,但HTML节点也可以用leet speak替换。

示例($ html是网页):

$find = array("a","b","c","d","e","f","g","h","i","j"."k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
$repl = array("4","b","c","d","3","f","g","h","1","j","k","1","m","n","0","p","9","r","5","7","u","v","w","x","y","2");
$html = str_replace($find, $repl, $html);

它也取代了HTML节点。

可以使用XPath和PHP使用XPath选择器 text()来完成吗?示例($ html是网页):

$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$xpath->query('//text()');
\\HERE THE REPLACING IN XPATH

1 个答案:

答案 0 :(得分:3)

试试这个:

$dom = new DOMDocument;
$dom->loadHTML( $html );
$xpath = new DOMXPath( $dom );
$nodes = $xpath->query( '//text()' );
foreach( $nodes as $node )
{
    $node->nodeValue = str_replace( $find, $repl, $node->nodeValue );
}
echo $dom->saveHTML();

请注意,这可能是一个更有用的xpath查询,可满足您的需求:

$nodes = $xpath->query( '//head/title/text() | //body//text()' );

...因为这只会替换<head><title>中的文字或<body>后代的文字。可能不想替换可能的样式,Javascript和你有什么。 ; - )


旁注:我已经用你的查找和替换字符数组进行了测试,但是有些东西正在发生,我无法弄明白。替换字符似乎并不总是与找到的字符对齐。我不知道为什么会这样。

我重新创建了数组,这些对我有用:

$find = array('a','b','c','d','e','f','g','h','i','j'.'k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$repl = array('4','b','c','d','3','f','g','h','1','j'.'k','1','m','n','0','p','9','r','5','7','u','v','w','x','y','2');

我无法弄清楚为什么你的数组为我工作。 : - /也许是一个编码问题?如果有人想要猜测并冒险猜测,请做。

编辑:正如rxdazn注意到的那样,"j"."k"是第一个数组中的问题,我完全忽略了,因为你可以从我重新创建的数组中看到(我将$ find复制到$ repl,替换引号,并填写leet characters)。