更改在 HTML 和 PHP 中生成 HREF 属性的文件中的文本
Changing text in file that generates HREF attribute in HTML with PHP
我需要能够更改文本文件中的文本,该文本文件用于通过表单在我的页面中生成 link 的 HREF 属性(即用户将 link 放入形式和 HTML 中的 link 将会改变)。到目前为止,我制作了那个表格和一个 PHP 脚本。它正在工作,只是它不仅会更改 HREF 属性,还会在 HTML 中创建一个全新的 link(即 <a href="Link"><img src="web/assets/images/youtube.png" alt="YT" width="42" height="42" border="0"></a>
)。
它是这样工作的——当你将 link 放入表单时,该脚本会将 link 写入 TXT 文件,然后在我需要 HREF 的那个页面上是另一个脚本从文本文件中取出 link。这是 PHP:
<?php
$subor = @file('youtube.txt');
$lines = sizeof($subor);
$count = $lines/1;
for ($i = 0; $i < $count; $i++)
{
?>
<a href="<?php echo ($subor[$i * 1 + 0]) ?>"><img
src="web/assets/images/youtube.png" alt="YT" width="42" height="42"
border="0"></a>
<?php
}
?>
这是将 link 从表单写入文本文件的脚本:
<?php
$myFile = "youtube.txt";
if(isset($_POST['flag']) && !empty($_POST['flag'])) {
$fileWrite = $_POST['flag'] . "\n";
}
if($fileWrite) {
$fh = fopen($myFile, "a") or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
exec('/your/command /dev/null 2>/dev/null &');
}
?>
<?php
header( 'Location: http://kraj.com/home.php' ) ;
?>
那么如何编辑该代码使其不会创建另一个代码但会更改其中的 href?
模式参数
So how to edit that code so it wont create another but it will change href inside that one ?
将传递给fopen()的第二个参数(即mode)更改为'w'
。这样指针将被放置在文件的开头,而不是将指针放在文件的末尾(用于追加)。
$fh = fopen($myFile, "w") or die("can't open file"); //Make sure you have permission
请参阅 this playground example 中的演示。
File_put_contents()
一种更简单的技术是使用 file_put_contents()。然后下面三行
$fh = fopen($myFile, "a") or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
可以替换为:
$numberOfBytesWritten = file_put_contents($myFile, $fileWrite);
我需要能够更改文本文件中的文本,该文本文件用于通过表单在我的页面中生成 link 的 HREF 属性(即用户将 link 放入形式和 HTML 中的 link 将会改变)。到目前为止,我制作了那个表格和一个 PHP 脚本。它正在工作,只是它不仅会更改 HREF 属性,还会在 HTML 中创建一个全新的 link(即 <a href="Link"><img src="web/assets/images/youtube.png" alt="YT" width="42" height="42" border="0"></a>
)。
它是这样工作的——当你将 link 放入表单时,该脚本会将 link 写入 TXT 文件,然后在我需要 HREF 的那个页面上是另一个脚本从文本文件中取出 link。这是 PHP:
<?php
$subor = @file('youtube.txt');
$lines = sizeof($subor);
$count = $lines/1;
for ($i = 0; $i < $count; $i++)
{
?>
<a href="<?php echo ($subor[$i * 1 + 0]) ?>"><img
src="web/assets/images/youtube.png" alt="YT" width="42" height="42"
border="0"></a>
<?php
}
?>
这是将 link 从表单写入文本文件的脚本:
<?php
$myFile = "youtube.txt";
if(isset($_POST['flag']) && !empty($_POST['flag'])) {
$fileWrite = $_POST['flag'] . "\n";
}
if($fileWrite) {
$fh = fopen($myFile, "a") or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
exec('/your/command /dev/null 2>/dev/null &');
}
?>
<?php
header( 'Location: http://kraj.com/home.php' ) ;
?>
那么如何编辑该代码使其不会创建另一个代码但会更改其中的 href?
模式参数
So how to edit that code so it wont create another but it will change href inside that one ?
将传递给fopen()的第二个参数(即mode)更改为'w'
。这样指针将被放置在文件的开头,而不是将指针放在文件的末尾(用于追加)。
$fh = fopen($myFile, "w") or die("can't open file"); //Make sure you have permission
请参阅 this playground example 中的演示。
File_put_contents()
一种更简单的技术是使用 file_put_contents()。然后下面三行
$fh = fopen($myFile, "a") or die("can't open file"); //Make sure you have permission
fwrite($fh, $fileWrite);
fclose($fh);
可以替换为:
$numberOfBytesWritten = file_put_contents($myFile, $fileWrite);