如何在此html输入字段中显示此php脚本生成的文本?

时间:2018-11-26 18:29:01

标签: php html

这是我的php代码:

<?php
     //Path of file
     $myFile = "test_file.txt";
     //Read file from array
     $lines = file($myFile);
     //Get number line of file
     $lineTotal = count($lines);
     //Remove 1 line (start from 0)
     $count = $lineTotal-1;
     //Get casual number
     $number_casual = rand(0,$count);
     //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
     echo $lines[$number_casual];
?>

此php代码从test_file.txt文件生成随机文本。 我想在此输入字段中显示此php代码随机生成的文本:

<input name="accesspin" style="width: 300px" value="the text will be here" size="36">

更新

这是我尝试过但无法正常工作的内容: 这是一个空白字段:

<?php
 //Path of file
 $myFile = "test_file.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
 echo $lines[$number_casual];
?>
<input name="accesspin" style="width: 300px" value="<?php echo htmlentities( $lines[$number_casual] ); ?>" size="36">

这也显示了一个空白字段:

<?php
 //Path of file
 $myFile = "test_file.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
 echo $lines[$number_casual];
?>

<input name="accesspin" style="width: 300px" value="<?php echo $lines[$number_casual]; ?>" size="36">

这使我的页面崩溃了

<?php
 //Path of file
 $myFile = "test_file.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
 echo '<input name="accesspin" style="width: 300px" value="' . htmlentities( $lines[$number_casual] ) . '" size="36">
?>

<input name="accesspin" style="width: 300px" value="<?php echo $lines[$number_casual]; ?>" size="36">

我的.txt文件不包含任何特殊字符。看起来像这样:

text1
text2
text3

更新2: 不好意思我不小心在php代码中输入了错误的.txt文件名,因此该字段为空白。这段代码有效:

<?php
 //Path of file
 $myFile = "random.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
?>
<input name="accesspin" style="width: 300px" value="<?php echo htmlentities( $lines[$number_casual] ); ?>" size="36">

3 个答案:

答案 0 :(得分:2)

只需添加到值标签中即可:

<?php
 //Path of file
 $myFile = "test_file.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
 echo $lines[$number_casual];
?>

<input name="accesspin" style="width: 300px" value="<?php echo $lines[$number_casual]; ?>" size="36">

答案 1 :(得分:2)

假设您的文本文件不包含正确编码的HTML内容,那么您需要确保使用htmlentities()

echo '<input name="accesspin" style="width: 300px" value="' . htmlentities( $lines[$number_casual] ) . '" size="36">';

<input name="accesspin" style="width: 300px" value="<?php echo htmlentities( $lines[$number_casual] ); ?>" size="36">

答案 2 :(得分:0)

将html添加到您的回声中。

 //Path of file
 $myFile = "test_file.txt";
 //Read file from array
 $lines = file($myFile);
 //Get number line of file
 $lineTotal = count($lines);
 //Remove 1 line (start from 0)
 $count = $lineTotal-1;
 //Get casual number
 $number_casual = rand(0,$count);
 //Print line 2 ([0] = 1 ; [1] = 2 ; ...)
 echo '<input name="accesspin" style="width: 300px" value="' . $lines[$number_casual]; . '" size="36">';