-1

Im completely new to PHP and have a simple questions. The first thing I have to solve is to change the Page-Title on my HTML-Website with PHP. Yes it has to be PHP or something that runs before the HTML is showed in browser. The Page-Title-Text that I want to choose as Title is located on my HTML page like <h4 class="fn"> Title of my Page </h4>. I got smth like this:

<?php
    $doc= DOMDocument::loadHTMLFile('index.html');
    $doc= new DOMDocument();
    $titelelem=$doc->getElementsByTagName('title')->item(0);
    $realtitleelem=$doc->getElementsByTagName('h4')->item(0);
    $realtitle=$realtitelelem->innerHTML;
    $titelelem->innerHTML=$realtitle;

    echo $doc->saveXML();
    echo $doc->saveHTML();
?>

No clue if smith is right here. Im not even sure how I load my html as new DOM.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Smeyer
  • 7
  • 1
  • For the part "Im not even sure how I load my html as new DOM". Take a look at the example in the docs: https://www.php.net/manual/en/domdocument.loadhtmlfile.php – Roland Starke Oct 29 '19 at 12:10

1 Answers1

-1

You could do it like this but this would just be tied to a static variable. Please provide more info if you want to do something different, like retrieve the title from a MYSQL table and/or alter it based on other data so it's dynamic.

<?php
$myVariable = "My Page Title";
?>

<html>
<head>
<title><?php echo $myVariable; ?></title>
</head>
<body>
<h4 class="fn"><?php echo $myVariable; ?></h4>
</body>
</html>
SJacks
  • 408
  • 3
  • 19
  • First of all thanks for the fast awnser! My idea is to take the innerHTML of

    , store it in a variable and than print out the variable as the innerHTML of the title tag. So if

    Mainpage

    the title should be "Mainpage" after executing the php code.
    – Smeyer Oct 29 '19 at 12:25
  • You can use a period to join two variables together. Like $var1="amazon"; $var2="monkey"; echo $var1.$var2; which would return "amazonmonkey". If you wanted to space the two words use an echo in quotes and a space between: echo "$var1 $var2"; Hope that helps. EDIT: Unless you're practicing or the data in the h4 tag is dynamic there's not much point in this. – SJacks Oct 29 '19 at 13:31