PHP DOM 解析器:查找所有链接的文本并更改它

发布于 2024-12-13 23:50:24 字数 538 浏览 0 评论 0原文

我是 PHO DOM 解析器的新手。我有一个像这样的字符串:

$coded_string = "Hello, my name is <a href="link1">Marco</a> and I'd like to <strong>change</strong> all <a href="link2">links</a> with a custom text";

并且我想使用自定义字符串更改链接中的所有文本(在示例中,Marco链接),比如 < em>你好。

我怎样才能在 PHP 上做到这一点?目前我只将 XDOM/XPATH 解析器初始化为:

$dom_document = new DOMDocument();      
$dom_document->loadHTML($coded_string);
$dom_xpath = new DOMXpath($dom_document);

I'm new into PHO DOM Parser. I have a string like this :

$coded_string = "Hello, my name is <a href="link1">Marco</a> and I'd like to <strong>change</strong> all <a href="link2">links</a> with a custom text";

and I'd like to change all text in the links (in the example, Marco and links) with a custom string, let say hello.

How can I do it on PHP? At the moment I only initilized the XDOM/XPATH parser as :

$dom_document = new DOMDocument();      
$dom_document->loadHTML($coded_string);
$dom_xpath = new DOMXpath($dom_document);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧人 2024-12-20 23:50:24

您对 xpath 在这里有很好的了解,下面的示例展示了如何选择所有 textnode 子节点 (DOMTextDocs) 的 元素并更改其文本:

$dom_document = new DOMDocument();      
$dom_document->loadHTML($coded_string);
$dom_xpath = new DOMXpath($dom_document);

$texts = $dom_xpath->query('//a/child::text()');
foreach ($texts as $text)
{
    $text->data = 'hello';
}

让我知道这是否有帮助。

You have a good sense for xpath here, the following example shows how to select all textnode children (DOMTextDocs) of the <a> elements and change their text:

$dom_document = new DOMDocument();      
$dom_document->loadHTML($coded_string);
$dom_xpath = new DOMXpath($dom_document);

$texts = $dom_xpath->query('//a/child::text()');
foreach ($texts as $text)
{
    $text->data = 'hello';
}

Let me know if this is helpful.

萌面超妹 2024-12-20 23:50:24

尝试 phpQuery ( http://code.google.com/p/phpquery/ ):

<?php

    $coded_string = 'Hello, my name is <a href="link1">Marco</a> and I\'d like to <strong>change</strong> all <a href="link2">links</a> with a custom text';

    require('phpQuery.php');

    $doc = phpQuery::newDocument($coded_string);
    $doc['a']->html('hello');
    print $doc;

?>

打印:

Hello, my name is <a href="link1">hello</a> and I'd like to <strong>change</strong> all <a href="link2">hello</a> with a custom text

Try phpQuery ( http://code.google.com/p/phpquery/ ):

<?php

    $coded_string = 'Hello, my name is <a href="link1">Marco</a> and I\'d like to <strong>change</strong> all <a href="link2">links</a> with a custom text';

    require('phpQuery.php');

    $doc = phpQuery::newDocument($coded_string);
    $doc['a']->html('hello');
    print $doc;

?>

Prints:

Hello, my name is <a href="link1">hello</a> and I'd like to <strong>change</strong> all <a href="link2">hello</a> with a custom text
岛歌少女 2024-12-20 23:50:24
<?php

$coded_string = "Hello, my name is <a href='link1'>Marco</a> and I'd like to <strong>change</strong> all <a href='link2'>links</a> with a custom text";

$dom_document = new DOMDocument();      
$dom_document->loadHTML($coded_string);
$dom_xpath = new DOMXpath($dom_document);

$links = $dom_xpath->query('//a');
foreach ($links as $link)
{
    $anchorText[] = $link->nodeValue;
}

$newCodedString = str_replace($anchorText, 'hello', $coded_string);

echo $newCodedString;
<?php

$coded_string = "Hello, my name is <a href='link1'>Marco</a> and I'd like to <strong>change</strong> all <a href='link2'>links</a> with a custom text";

$dom_document = new DOMDocument();      
$dom_document->loadHTML($coded_string);
$dom_xpath = new DOMXpath($dom_document);

$links = $dom_xpath->query('//a');
foreach ($links as $link)
{
    $anchorText[] = $link->nodeValue;
}

$newCodedString = str_replace($anchorText, 'hello', $coded_string);

echo $newCodedString;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文