English 中文(简体)
Using SimpleHtmlDom, how to remove and replace a specific attribute
原标题:

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?

最佳回答

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();
问题回答

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.

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




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签