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.

Help Convert This Link Extractor To Procedural Style Php

Discussion in 'PHP' started by Buddy Programmer, Sep 10, 2019.

  1. #1
    Hiya!

    I just registered now. :)
    Can someone be kind enough to convert this code to procedural style php and past it here so I can learn the code aswell as all other newbies ?
    You see, I am still learning procedural style and have not gone to the oop level yet.
    It is a Link Extractor.
    
    <?php
    
    //Code from: https://potentpages.com/web-crawler-development/tutorials/php/techniques
    
    //Assuming your contents are in a variable called $contents
    //New DOM Document
    $document = new DOMDocument;
    //Load HTML in $contents variable
    $document->loadHTML($contents);
    //Get all links
    if($links = $document->getElementsByTagName('img')) {
      //Loop through all links
      foreach($links as $node) {
      //Get source of the image (src attribute)
      $img_src = $node->getAttribute('src');
      //Get alt text of the image (alt attribute)
      $img_alt = $node->getAttribute('alt');
      }
    }
    
    ?>
    
    Code (markup):
     
    Buddy Programmer, Sep 10, 2019 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    That is entirely procedural using built in php functions. Can you explain more what you're needing?
     
    jestep, Sep 10, 2019 IP
    sarahk likes this.
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    Totally and utterly incorrect. To the point of likely not even knowing enough PHP to try and answer this poor sod's question!

    That code is using PHP's built in DOMDocument OBJECT and its associated METHODS!

    Unfortunately for the OP, DOMDocument is inherently object based, since it turns HTML into the Document Object Model -- DOM. It is INHERENTLY object based just as HTML itself is. As such, there is no such thing as a procedural equivalent.

    ... and to be frank, even ASKING for a procedural equivalent likely means our little buddy hasn't learned enough HTML to be working with PHP yet.

    HTML is at its heart a plaintext representation of an object node tree. Each HTML element or section of context text is an object node, which has properties such as alt, src, id, class, type, etc, etc. A node in a tree can have parents, siblings, and children that are both objects in their own right, and properties of the relative element.

    This is why they are processed as objects, as they are objects. There is no such thing as a procedural equivalent and even attempting to do so would be an outright mentally enfeebled waste of time. Kind of like the mental midgetry of mysqli's procedural wrappers, just don't go there.

    You want to get into HTML processing this way, it's time to man up, strap up, put on the big boy pants, and learn objects.

    Laugh being this is entirely how it's handled in JavaScript, something else you should have a basic command of before diving into PHP. It's hypertext preprocessor -- it exists to be glue between data and markup... as such if you don't know the basics of HTML just what are you going to use it for?
     
    Last edited: Sep 10, 2019
    deathshadow, Sep 10, 2019 IP
    JEET likes this.
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    $this->isNot('Procedural'); // duh...
     
    NetStar, Sep 15, 2019 IP
    deathshadow likes this.