setAttribute被忽略了,为什么?

The intention of this code is that an the HTML-attribute 'data-original-src' is added to the img-tag, in case of unsecure http:// URL's. But this attribute is not generated.

What's wrong, and what mistake do I overlook? Anyway, the str_replace() is working like a charm.

public function parse($string) {
    if($this->settings['camo_enabled'] == true) {
        $doc = new DOMDocument();
        $doc->loadHTML($string);
        $images = $doc->getElementsByTagName('img');
        //$imgarray = array();
        foreach ($images as $image) {
            $url = $image->getAttribute('src');
            if(substr($url, 0, 8) != 'https://') {
                $image->setAttribute('data-original-src', $url);
                $camo = $this->proxy_url($url);
                $string = str_replace($image->getAttribute('src'), $camo, $string);
                unset($url, $camo);
            }
        }
    }
    return $string;
}

I think that the problem arises from the fact that you are forgetting to modify the $string variable you pass to the method calling saveHTML() before returning it. Commit your changes as follows:

public function parse($string)
{
    if ($this->settings['camo_enabled'] == true)
    {
        $doc = new DOMDocument();
        $doc->loadHTML($string);
        $images = $doc->getElementsByTagName('img');

        foreach ($images as $image)
        {
            $url = $image->getAttribute('src');

            if (substr($url, 0, 8) != 'https://')
            {
                $image->setAttribute('data-original-src', $url);
                $camo = $this->proxy_url($url);
                $string = str_replace($image->getAttribute('src'), $camo, $string);
                unset($url, $camo);
            }
        }
    }

    $string = $doc->saveHTML(); 
    return $string;

    // The two lines above can also be simply rewritten as:
    // return $doc->saveHTML();
}

For more information, refer to the official documentation.

On a side note, since you are performing changes in both the $string variable and in the parsed HTML file, stick to the latter by modifying this line:

$string = str_replace($image->getAttribute('src'), $camo, $string);

into:

$image->setAttribute('src', $camo);