duandaiqin6080 2013-05-06 08:08
浏览 30
已采纳

为每个.text文件行创建一个新的链接与PHP中的.text文件行数据[关闭]

I would like to create a .php file that will read a .txt file and then for each line in the text file it will take the data and create a new link with the data.

Example:

list.txt

r2389
y3323
u3330
d2723

reader.php

<?php
    $myFile = "list.txt";
    $lines = file($myFile);
?>
<a href="http://website.com/<?php echo $lines[0];?>">link 1</a>
<a href="http://website.com/<?php echo $lines[1];?>">link 2</a>
<a href="http://website.com/<?php echo $lines[2];?>">link 3</a>
<a href="http://website.com/<?php echo $lines[3];?>">link 4</a>

reader.php output

<a href="http://website.com/r2389">link 1</a>
<a href="http://website.com/y3323">link 2</a>
<a href="http://website.com/u3330">link 3</a>
<a href="http://website.com/d2723">link 4</a>



This will get the information and then echo it into the link.
But this means I need to create the right amount of links for it to work properly, in the end I want to just be able to edit the list.txt and then have the php automatically create a

<a href="<?php echo $lines[*];?>">link *</a>

depending on the amount of lines in the list.txt. It will also label

<a>link *</a>

depending on the line number.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dsnrixf6765 2013-05-06 08:13
    关注

    Just loop through the array.

    <?php
        $myFile = "list.txt";
        $lines = file($myFile);
    
        $lineNumber = 1;
        foreach($lines as $line)
        {
        ?>
        <a href="http://website.com/<?php echo $line;?>">link <?php echo $lineNumber?></a>
        <?
        $lineNumber++;
        }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址