1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

addAttribute Problem

Discussion in 'PHP' started by Jeremy Benson, Oct 12, 2018.

  1. #1
    Hello,

    I have a bit of html with images. Using DOMDocument I'd like to add class imgSizeFixed if the image is wider than 730px. The code below works perfect except for adding the class. It goes inside the if statement fine, but the class is never added to the image.

    if(!empty($article))
                                                {
                                          
                                                    include('php/classes/bbCode.php');
                                                  
                                                    $articleText = bbCode::addCodes($article['article']);
                                                                                              
                                                    $html = '<html><body>' . $articleText . '</body></html>';
                                                                                                                                  
                                                    $dom = new DOMDocument();
                                                    $dom->loadHTML($html);
                                                  
                                                    $domNodeList = $dom->getElementsByTagName("img");
    
                                                    foreach ($domNodeList as $domNode)
                                                    {
                                                      
                                                        $attrs = array();
                                                        $attrs = $domNode->getAttribute('src');
                                                      
                                                      
                                                        $imageArray = getimagesize($attrs);
                                                      
                                                      
                                                      
                                                        if(!empty($imageArray) && $imageArray != false)
                                                        {
                                                          
                                                            if($imageArray[0] > 730)
                                                            {
                                                              
                                                                // image to big add class imgSizeFixed
                                                              
                                                                $domNode->setAttribute('class', 'imgSizeFixed');
                                                      
                                                                // end image too big add class imgSizeFixed
                                                            }
                                                          
                                                        }
                                                      
                                                   
                                                    }
                                                  
                                                    // remove <html><body></html></body>
    
                                                    $html = substr($html, 12);
                                                    $html = substr($html, 0, -14);
                                                  
                                                    echo $html;
                                                  
                                                    echo '<div style="margin-bottom:48px;"></div>';
                                               
                                                }
    Code (markup):
     
    Last edited: Oct 12, 2018
    Jeremy Benson, Oct 12, 2018 IP
  2. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #2
    This is solved. Called $html = $dom->saveHTML();
     
    Last edited: Oct 12, 2018
    Jeremy Benson, Oct 12, 2018 IP