gpt4 book ai didi

php - 将 HTML 文本转换为 Leet (1337) Speak with XPath

转载 作者:太空狗 更新时间:2023-10-29 15:11:48 27 4
gpt4 key购买 nike

我想将网页转换为 leet (1337) speak with 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

最佳答案

试试这个:

$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 字符)。

关于php - 将 HTML 文本转换为 Leet (1337) Speak with XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13532025/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com