在文件中添加一行
Adding a line into a file
所以我有这个自动生成的 HTML 文件。
http://pastebin.com/mTMJNrdm
我正在尝试编写一个 PHP 脚本来替换 第 5 行 。
<LINK href="style.css" rel="stylesheet" type="text/css"></style>
因为它是一个文件,所以我正在努力思考它。什么函数可以为我完成这个?
当前代码:
$fh = file('standings.html');
$css = addslashes('<LINK href="style.css" rel="stylesheet" type="text/css"></style>');
$pattern = "</style>";
foreach ($fh as $lines) {
if (preg_match($pattern, $lines)) {
// Replace this line with $css
}
}
php file
函数已经将整个文件读入数组。只需通过指定所需的索引来替换所需的行:
$lines = file('standings.html');
$lines[4] = addslashes('<LINK href="style.css" rel="stylesheet" type="text/css"></style>');
$new_content = implode(" ", $lines);
file_put_contents('standings.html', $new_content);
检查此代码,我注释 fopen 和 fwrite,检查文件权限并取消注释。
<?php
$dom = new DOMDocument;
$dom->loadHTMLFile('file.html');
$elements = $dom->getElementsByTagName('style');
for ($i = $elements->length; --$i >= 0; ) {
$s = $elements->item($i);
$element = $dom->createElement('style', '');
$domAttribute = $dom->createAttribute('href');
$domAttribute->value = 'style.css';
$element->appendChild($domAttribute);
$domAttribute = $dom->createAttribute('rel');
$domAttribute->value = 'stylesheet';
$element->appendChild($domAttribute);
$domAttribute = $dom->createAttribute('type');
$domAttribute->value = 'text/css';
$element->appendChild($domAttribute);
$s->parentNode->replaceChild($element, $s);
}
$html = $dom->saveHTML();
/*
$file = fopen("file.html", "w+");
fwrite($file, $html);
*/
print_r("<pre>");
print_r($html);
print_r("</pre>");
die;
所以我有这个自动生成的 HTML 文件。 http://pastebin.com/mTMJNrdm
我正在尝试编写一个 PHP 脚本来替换 第 5 行 。
<LINK href="style.css" rel="stylesheet" type="text/css"></style>
因为它是一个文件,所以我正在努力思考它。什么函数可以为我完成这个?
当前代码:
$fh = file('standings.html');
$css = addslashes('<LINK href="style.css" rel="stylesheet" type="text/css"></style>');
$pattern = "</style>";
foreach ($fh as $lines) {
if (preg_match($pattern, $lines)) {
// Replace this line with $css
}
}
php file
函数已经将整个文件读入数组。只需通过指定所需的索引来替换所需的行:
$lines = file('standings.html');
$lines[4] = addslashes('<LINK href="style.css" rel="stylesheet" type="text/css"></style>');
$new_content = implode(" ", $lines);
file_put_contents('standings.html', $new_content);
检查此代码,我注释 fopen 和 fwrite,检查文件权限并取消注释。
<?php
$dom = new DOMDocument;
$dom->loadHTMLFile('file.html');
$elements = $dom->getElementsByTagName('style');
for ($i = $elements->length; --$i >= 0; ) {
$s = $elements->item($i);
$element = $dom->createElement('style', '');
$domAttribute = $dom->createAttribute('href');
$domAttribute->value = 'style.css';
$element->appendChild($domAttribute);
$domAttribute = $dom->createAttribute('rel');
$domAttribute->value = 'stylesheet';
$element->appendChild($domAttribute);
$domAttribute = $dom->createAttribute('type');
$domAttribute->value = 'text/css';
$element->appendChild($domAttribute);
$s->parentNode->replaceChild($element, $s);
}
$html = $dom->saveHTML();
/*
$file = fopen("file.html", "w+");
fwrite($file, $html);
*/
print_r("<pre>");
print_r($html);
print_r("</pre>");
die;