我目前正在使用这个HTML DOM PARSER使用php:http://simplehtmldom.sourceforge.net/
我对如何删除和替换所选属性href="style.css"
感到困惑,我想用"index/style.css"
替换链接,我应该只插入
index /
或从整个html代码中替换整个属性?
答案 0 :(得分:12)
这应该这样做:
$doc = str_get_html($code);
foreach ($doc->find('a[href]') as $a) {
$href = $a->href;
if (/* $href begins with a relative URL path */) {
$a->href = 'index/'.$href;
}
}
$code = (string) $doc;
您也可以使用PHP’s native DOM library:
$doc = new DOMDocument();
$doc->loadHTML($code);
$xpath = new DOMXpath($doc);
foreach ($xpath->query('//a[@href]') as $a) {
$href = $a->getAttribute('href');
if (/* $href begins with a relative URL path */) {
$a->setAttribute('href', 'index/'.$href);
}
}
$code = $doc->saveHTML();
答案 1 :(得分:1)
官方手册有几个基本涵盖你所需要的例子:
http://simplehtmldom.sourceforge.net/manual.htm
如果您遇到某些特定步骤的问题,请随时更新您的问题并提供一些代码。
答案 2 :(得分:0)
$html = str_get_html($string);
if ($html){ // Verify connection, return False if could not load the resource
$e = $html->find("a");
foreach ($e as $e_element){
$old_href = $e_element->outertext;
// Do your modification in here
$e_element->href = affiliate($e_element->href); // for example I replace original link by the return of custom function named 'affiliate'
$e_element->href = ""; //remove href
$e_element->target .= "_blank"; // I added target _blank to open in new tab
// end modification
$html = str_replace($old_href, $e_element->outertext, $html); // Update the href
}