Using SimpleHtmlDom, how to remove and replace a specific attribute

こ雲淡風輕ζ 提交于 2019-11-28 11:34:59

问题


I'm currently using this HTML DOM PARSER using php : http://simplehtmldom.sourceforge.net/

I'm confused on how to remove and replace the selected attribute href="style.css", I want to replace the link with "index/style.css", should I insert only the

index/

or replace the whole attribute from the whole html code?


回答1:


This should do it:

$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;

You could also use 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();



回答2:


The official manual has several examples that basically cover all you need:

http://simplehtmldom.sourceforge.net/manual.htm

If you have issues with some specific step, feel free to update your question and provide some of your code.




回答3:


$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
    }


来源:https://stackoverflow.com/questions/4600928/using-simplehtmldom-how-to-remove-and-replace-a-specific-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!