【问题标题】:How to show this php script generated text inside this html input field?如何在这个 html 输入字段中显示这个 php 脚本生成的文本?
【发布时间】:2019-04-28 10:42:54
【问题描述】:

这是我的 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">

【问题讨论】:

标签: php html


【解决方案1】:

只需添加到值标签:

<?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">

【讨论】:

  • echo $lines[$number_casual]; 打印给你什么?
  • .txt 文件中的随机文本
  • 此解决方案有效,除非您有特殊字符。您可以使用htmlentities() 尝试@MonkeyZeus 解决方案。
【解决方案2】:

将 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">';

【讨论】:

    【解决方案3】:

    假设您的文本文件不包含正确编码的 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">
    

    【讨论】:

    • 它们都不起作用。请更新我的问题。
    • @user8481790 您的echo 缺少单引号和分号;这是一个常见的语法错误。您可以修复 echo 或仅使用我的第二个示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2021-06-14
    • 1970-01-01
    相关资源
    最近更新 更多