Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过PHP附加HTML元素_Php_Html - Fatal编程技术网

通过PHP附加HTML元素

通过PHP附加HTML元素,php,html,Php,Html,我想使用PHPcode在外部Html文件中附加我的head标记和script标记(带有一些内容)。 但是我的Html没有更新或显示任何错误 PHP代码: Html文件: (一个简单的html模式) 我已经参考了文件 仍然无法解决问题。给定了一个非常简单的HTML文件(simple.HTML) 一个简单的HTML页面 简单HTML 这太好了 然后使用以下命令 $file='simple.html'; libxml_use_internal_errors( true ); $dom=

我想使用
PHP
code在外部
Html
文件中附加我的
head
标记和
script
标记(带有一些内容)。 但是我的
Html
没有更新或显示任何错误

PHP代码:

Html文件: (一个简单的html模式)


我已经参考了文件
仍然无法解决问题。

给定了一个非常简单的HTML文件(
simple.HTML


一个简单的HTML页面
简单HTML
这太好了

然后使用以下命令

$file='simple.html';


libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->recover=true;
$dom->strictErrorChecking=false;
$dom->loadHTMLFile( $file );
$errors = libxml_get_errors();
libxml_clear_errors();




$script=$dom->createElement('script');
$script->textContent='/* Hello World */';

/* use [] notation rather than ->item(0) */
$dom->getElementsByTagName('head')[0]->appendChild( $script );



printf('<pre>%s</pre>',htmlentities( $dom->saveHTML() ));

/* write changes back to the html file - ie: save */
$dom->saveHTMLFile( $file );
$file='simple.html';
libxml\u使用\u内部错误(true);
$dom=新的DOMDocument;
$dom->validateOnParse=false;
$dom->recover=true;
$dom->strigerrorchecking=false;
$dom->loadHTMLFile($file);
$errors=libxml_get_errors();
libxml_clear_errors();
$script=$dom->createElement('script');
$script->textContent='/*Hello World*/';
/*使用[]符号而不是->项(0)*/
$dom->getElementsByTagName('head')[0]->appendChild($script);
printf(“%s”,htmlentities($dom->saveHTML());
/*将更改写回html文件-即:保存*/
$dom->saveHTMLFile($file);
将屈服(用于显示)


一个简单的HTML页面
简单HTML
这太好了


echo语句显示了什么?工作正常您是否有这样的印象:
$doc->saveXML()
会更改您最初从中读取HTML代码的磁盘上的HTML文件…?@KoalaYeung不显示任何内容。printf显示HTML工作正常,但是html文件没有进行类似的更新,最后需要再次保存该文件。。我会编辑答案很好-很高兴它有帮助
<html>
<head></head>
<body></body>
</html> 
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>A simple HTML Page</title>
    </head>
    <body>
        <h1>Simple HTML</h1>
        <p>Well this is nice!</p>
    </body>
</html>
$file='simple.html';


libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->recover=true;
$dom->strictErrorChecking=false;
$dom->loadHTMLFile( $file );
$errors = libxml_get_errors();
libxml_clear_errors();




$script=$dom->createElement('script');
$script->textContent='/* Hello World */';

/* use [] notation rather than ->item(0) */
$dom->getElementsByTagName('head')[0]->appendChild( $script );



printf('<pre>%s</pre>',htmlentities( $dom->saveHTML() ));

/* write changes back to the html file - ie: save */
$dom->saveHTMLFile( $file );
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>A simple HTML Page</title>
    <script></script></head>
    <body>
        <h1>Simple HTML</h1>
        <p>Well this is nice!</p>
    </body>
</html>