-2

Just trying to edit/modify the head tag in order to add something inside with DOM and PHP.

    $dom = new DOMDocument();
$dom->loadHtml(utf8_decode($html), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

for($i=0; $i<count($r);$i++) 
{
    
    // Prepare the HTML to insert
    Here I want to add $var inside head tag (at the end if possible)
}

return $dom->saveHTML();

Everytime I tried, I have LENGHT=0 as the result of var_dump.

Edit: I don't want to edit an existing tag. I want to add a new one. To be more specific, I need to add OG meta tag for Facebook sharing.

Edit2 as requested :

Before

<head>
    <meta blabla>
    <title></title>
</head>
<body>
    <h1></h1>
</body>

After

<head>
    <meta blabla>
    <title></title>
    <meta new1>
</head>
<body>
    <h1></h1>
</body>

But need to be edit via DOMDocument in PHP...

OverSu
  • 110
  • 1
  • 8
  • `without any lib`...does that include not using `DOMDocument`? And what have you tried so far? Where did you get stuck? We'll help you, but we're not a free write-my-code service. Try to ask a specific question about a specific problem, not just give us a general requirement. See also [ask]. – ADyson Mar 15 '22 at 14:37
  • Sorry. Trying since a few hours with a lot of help from other post without found the solution. So, only with DomDocument lib... Message written too quickly ! Eveytime I'm trying to get the head tag content, I have LENGHT 0 as the result of var_dump. – OverSu Mar 15 '22 at 14:51
  • 1
    Please show exactly what you tried, which produced that result. – ADyson Mar 15 '22 at 14:53
  • I had try a lot of thing and delete when it didn't work. So, I didn't have anything workable. – OverSu Mar 15 '22 at 15:23
  • Can you edit the question and show a **simplified** version of the html before and after? – Jack Fleeting Mar 15 '22 at 15:45
  • This is done ;) – OverSu Mar 15 '22 at 15:53

2 Answers2

0

Add this to the top of your file:

<?php 
     $var = "Hello world.";
?>

Then start the HTML and add it there.

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $var ?></title>
</head>
<body>
    
</body>
</html>

If you want to do it in PHP, you can try to use:

$titles = $domDocument->getElementsByTagName('title');
foreach($titles as $key => $title){
    $title->setAttribute('attribute', 'value')
}

Source for the edit: https://stackoverflow.com/a/3195048/12077975

Zegert
  • 113
  • 10
  • I don't want to edit an existing tag. I want to add a new one. To be more specific, I need to add OG meta tag for Facebook sharing. But that's the idea ! – OverSu Mar 15 '22 at 15:23
0

Try something along these lines:

$before= 
'<html>
<head>
    <meta name="old"/>
    <title></title>
</head>
<body>
    <h1></h1>
</body>
</html>
';
$HTMLDoc = new DOMDocument();
$HTMLDoc->loadHTML($before, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD );   
$xpath = new DOMXPath($HTMLDoc);

$destination = $xpath->query('//head/title');
$template = $HTMLDoc->createDocumentFragment();
$template->appendXML('<meta name="new"/>');
$destination[0]->parentNode->insertBefore($template, $destination[0]->nextSibling);
echo $HTMLDoc->saveHTML();

Output:

<html>
<head>
    <meta name="old">
    <title></title><meta name="new">
</head>
<body>
    <h1></h1>
</body>
</html
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • Does the xpath need to load a new lib ? My project need to run on a lot of different server (nginx, apache, and more) without editing PHP or Server file... So, just need to know ! Or is it possible without XPath ? Don't know. – OverSu Mar 16 '22 at 09:22
  • @OverSu What do you mean by "load a new lib"? Both DOMDocument and DOMXpath are built in functions. Whatever supports one should support the other. – Jack Fleeting Mar 16 '22 at 10:24
  • That's great. The "load a new lib" thing is a requirement given to me by the IT dept. :/ – OverSu Mar 16 '22 at 15:02
  • $destination give me this on debug : DOMNodeList Object([length] => 0) and error : Trying to get property 'parentNode' of non-object. That's really strange. – OverSu Mar 16 '22 at 16:35
  • I don't why you're getting the error; [take a look here](https://3v4l.org/2EdrS). – Jack Fleeting Mar 16 '22 at 16:53
  • After a check, I can understand it better. The script use ob_start(); and ob_get_clean();. That's why it's doesn't works. Hmmm, I need to try something else. – OverSu Mar 16 '22 at 17:10