Change tag attribute value with PHP DOMDocument

后端 未结 2 732
無奈伤痛
無奈伤痛 2020-11-30 09:53

I want to change the value of the attribute of a tag with PHP DOMDocument.

For example, say we have this line of HTML:



        
2条回答
  •  伪装坚强ぢ
    2020-11-30 10:12

    $dom = new domDocument;
    $dom->loadHTML('Click here');
    
    $elements = $dom->getElementsByTagName( 'a' );
    
    if($elements instanceof DOMNodeList)
        foreach($elements as $domElement)
            $domElement->setAttribute('href', 'http://www.google.com/');
    

提交回复
热议问题