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.

get element by tag name value in PHP

Discussion in 'PHP' started by bhavatmaj, Sep 26, 2012.

  1. #1
    What I wish to echo in PHP is the content/value in <title> </title> tag on the same page

    
    <hrad>
    <meta name="Description" content=""/> 
    <meta name="Keywords" content=""/> 
    
    <title>abc what I wish to display</title>
    </head>
    HTML:

    and I wish to echo "abc abc what I wish to display" through PHP syntax
    what I am trying but it is not working
    <?php 
    $dom = new DOMDocument;
    $title= $dom->getElementsByTagName('title');
    echo $title;
    ?>
    HTML:

    Please help
     
    bhavatmaj, Sep 26, 2012 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
  3. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #3
    Probably because you are using DOMDocument() wrong. Do the following:

    
    <?php
    
    $html = <<<EOF
    <head>
    <meta name="Description" content=""/>
    <meta name="Keywords" content=""/>
    
    <title>abc what I wish to display</title>
    </head>
    EOF;
    
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $nodes = $dom->getElementsByTagName('title');
    $title = $nodes->item(0)->nodeValue;
    
    print $title;
    
    ?>
    PHP:
     
    NetStar, Sep 29, 2012 IP
  4. Vick.Kumar

    Vick.Kumar Active Member

    Messages:
    138
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    90
    #4
    What you can do is create a 'global.php' file and put your title in there for example.

    1. global.php
    
    <?php
    $sitetitle = "Your title here";
    ?>
    
    HTML:
    Then you can display it globally on as many pages as you want by doing this below.

    2. Index.php
    
    <?php
    require('global.php');
    ?>
    <html>
    <head>
    <title><?php echo $sitetitle; ?></title>
    </head>
    <body></body>
    </html>
    
    HTML:
     
    Vick.Kumar, Sep 30, 2012 IP
  5. Neeraj Dhiman

    Neeraj Dhiman Member

    Messages:
    15
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    41
    #5
    http://php.net/manual/en/domdocument.getelementsbytagname.php

    Hope that It will owrk :)
     
    Neeraj Dhiman, Feb 24, 2016 IP