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
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:
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: