Using SimpleHtmlDom, how to remove and replace a specific attribute

后端 未结 3 809
囚心锁ツ
囚心锁ツ 2020-12-21 21:41

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 h

3条回答
  •  感情败类
    2020-12-21 22:10

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

提交回复
热议问题