使用SimpleHTMLDom库获取所有href链接并在'fly'上更改这些链接

Zab*_*abs 1 html php parsing

我想要访问html字符串中的所有href链接并转换所有链接,如下所示:

<a href='www.google.com'>Google</a>
Run Code Online (Sandbox Code Playgroud)

会改变看起来像这样......

<a href='www.mysite.com/link.php?URL=www.google.com'>Google</a>
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议我这样做吗?

<?php
Run Code Online (Sandbox Code Playgroud)

require_once( 'simple_html_dom.php');

// load the class
$html = new simple_html_dom();

// load the entire string containing everything user entered here

$string = "<html><body><base href='http://www.site.biz/clients/g/'><a href='www.google.co.uk'>Google</a><a href='http://www.yahoo.co.uk'>Yahoo</a></body></html>";
$return = $html->load($string);

$links = $html->find('a');

foreach ($links as $link) 
{
    var_dump($link);
}
Run Code Online (Sandbox Code Playgroud)

?>

MrC*_*ode 5

你尝试过类似的东西吗?

$links = $html->find('a');

foreach ($links as $link) 
{
    if(isset($link->href))
    {
        $link->href = 'www.mysite.com/link.php?URL=' . $link->href;
    }
}

$newHTML = $html->save();

// $newHTML now contains the modified HTML
Run Code Online (Sandbox Code Playgroud)