【问题标题】:How to convert TXT file contents to URL's using PHP? [closed]如何使用 PHP 将 TXT 文件内容转换为 URL? [关闭]
【发布时间】:2014-09-01 14:53:35
【问题描述】:

我是 PHP 新手。

例如,我有一个不同名称的测试文件“names.txt”;

Maddox
Magnus
Malcolm
Melvin
Marcus
Mark
Marlon
Martin
Marvin
Matthew
Maurice
Max
Medwin
Melville
Merlin
and so on to 100s

我希望这些名称与 URL 一样位于另一个或相同的 TXT 文件中。

<a href="http://example.com/Maddox/something/">Maddox</a></br>
<a href="http://example.com/Magnus/something/">Magnus</a></br>
<a href="http://example.com/Malcolm/something/">Malcolm</a></br>
<a href="http://example.com/Melvin/something/">Melvin</a></br>
<a href="http://example.com/Milburn/something/">Milburn</a></br>
<a href="http://example.com/Marvin/something/">Marvin</a></br>
<a href="http://example.com/Melville/something/">Melville</a></br>
<a href="http://example.com/Montague/something/">Montague</a></br>
and so on to the all names of txt file.

我无法手动完成 100 次,所以问题是我如何使用 PHP 来做到这一点?

你能为此编写一个 PHP 代码吗?

非常感谢。

【问题讨论】:

标签: php


【解决方案1】:
$fp=file("filename");
    foreach($fp as $line)
    {

            echo '<a href="http://example.com/'.$line.'/something/">'.$line.'</a>'; 
            echo "<br>";

    }

【讨论】:

    【解决方案2】:
    $fp=file("filename");
        foreach($fp as $name)  
        {
    
            echo '<a href="http://example.com/'.trim($name).'/something/">'.$name.'</a>';   
            echo "<br>";
    
        }
    

    【讨论】:

    • 链接网址也应该包含name。请参阅 OP 的示例。
    • 感谢指点@HAL9000
    • 在 echo 'example.com/'.$line.'/something/…>' 行出现语法错误;
    • 我能够正确使用@Tristup。你的太好了。
    • 这不是我的代码 echo '
    猜你喜欢
    • 2020-08-31
    • 2017-07-13
    • 2016-05-25
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 2011-06-07
    相关资源
    最近更新 更多