www.webdeveloper.com
Results 1 to 5 of 5

Thread: putting meta tag inside the head tag using php.

  1. #1
    Join Date
    May 2010
    Location
    Manila, Philippines
    Posts
    189

    putting meta tag inside the head tag using php.

    Hi guys,

    I have the following codes of php. what it does was open the file myhtml.html and then puts a meta tag inside the html and then save the file.

    PHP Code:
    $doc = new DOMDocument("1.0");
    $doc->loadHTMLFile("myhtml.html");

    $node $doc->createElement("meta");
    $newnode $doc->appendChild($node);
    $newnode->setAttribute("name","description");
    $newnode->setAttribute("content""just a test descriptionzzz");
    $xx $newnode->getAttribute("content");
    $doc->saveHTMLFile("myhtml.html"); 

    but when i checked the myhtml.html file the code was like this :

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    <title>this is ok with me</title>
    </head> 
    <body>
        <div id="main-content">sample content of the edited html.</div>
        <span name="spsp">hello world!</span>
    </body>
    </html>
    <meta name="description" content="just a test descriptionzzz">
    which makes the meta tag i put into the file outside the html tag. how can i make it inside the head tag? anyone please.. thanks in advance.

  2. #2
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    PHP Code:
        $doc = new DOMDocument("1.0");
        
    $doc->loadHTMLFile("myhtml.html");
        
        
    $head document.getElementsByTagName ('head')->item(0);
        
    $meta $head->appendChild ($doc->createElement ('meta'));
        
    $meta->setAttribute("name","description");
        
    $meta->setAttribute("content""just a test descriptionzzz"); 
    Or something more like that.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  3. #3
    Join Date
    May 2010
    Location
    Manila, Philippines
    Posts
    189
    wow. this really works! thank you so much charles!

  4. #4
    Join Date
    May 2010
    Location
    Manila, Philippines
    Posts
    189
    although the line :

    PHP Code:
    $head document.getElementsByTagName ('head')->item(0); 
    should instead be :

    PHP Code:
    $head $doc->getElementsByTagName('head')->item(0); 

    thank you so much charles!

  5. #5
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Sorry about that. I had a bit of JavaScript floating about in my head.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles