给定一个用 PHP 回车符组织的文本文件,生成 HTML

发布于 2024-09-09 05:25:51 字数 371 浏览 1 评论 0原文

我希望能够在 PHP 中接收一个文件

**example_file.txt**
United States
Canada
-----------------------
Albania
Algeria
American Samoa
Andorra
Angola

,并将这些单独的换行符包装在我传递给它的 HTML 元素中。

例子:

magicHtmlGenerator(example_file.txt, '<li>') 
// spits out <li>United States</li><li>Canada</li> etc

I'd like to be able to take in a file in PHP

**example_file.txt**
United States
Canada
-----------------------
Albania
Algeria
American Samoa
Andorra
Angola

and wrap these individual linebreaks in an HTML element that I pass it.

Example:

magicHtmlGenerator(example_file.txt, '<li>') 
// spits out <li>United States</li><li>Canada</li> etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

惯饮孤独 2024-09-16 05:25:52
function magicHtmlGenerator($filename, $wrapper) {
    $x = file_get_contents($filename);
    return '<'.$wrapper.'>'.str_replace("\n",'</'.$wrapper.'><'.$wrapper.'>',$x).'</'.$wrapper.'>';
}

$html = magicHtmlGenerator('example_file.txt','li');
echo $html;
function magicHtmlGenerator($filename, $wrapper) {
    $x = file_get_contents($filename);
    return '<'.$wrapper.'>'.str_replace("\n",'</'.$wrapper.'><'.$wrapper.'>',$x).'</'.$wrapper.'>';
}

$html = magicHtmlGenerator('example_file.txt','li');
echo $html;
夏了南城 2024-09-16 05:25:52

加载文件,然后使用正则表达式进行替换。

preg_replace  ( \\r+([^\r]+)\r+\g  , \<li>$1</li>  ,  $str );

http://php.net/manual/en/function.preg-replace。 php

Load the file in and then use a regular expression to do the replacement.

preg_replace  ( \\r+([^\r]+)\r+\g  , \<li>$1</li>  ,  $str );

http://php.net/manual/en/function.preg-replace.php

怼怹恏 2024-09-16 05:25:52

创建一个接受这两个参数的函数,加载行(可以通过 file 函数轻松完成),然后迭代它们,将它们附加到字符串中,并用您想要的 HTML 标签填充。

Create a function which takes those two arguments, loads the lines (can be done easily via the file function), then iterate over them appending them to a string, padded with the HTML tag you want.

浅忆 2024-09-16 05:25:52

这是另一种方式:

$sxml = new SimpleXMLElement('<ul></ul>', LIBXML_NOXMLDECL);
$data = file('example_file.txt', FILE_IGNORE_NEW_LINES);
foreach ($data as $line) {
    if (ctype_alpha($line)) {   // Or whatever test you need
        $sxml->addChild('li', $line);
    }
}
echo $sxml->asXML();

输出:

  • 加拿大
  • 阿尔巴尼亚
  • 阿尔及利亚
  • 安道尔
  • 安哥拉

Here's another way:

$sxml = new SimpleXMLElement('<ul></ul>', LIBXML_NOXMLDECL);
$data = file('example_file.txt', FILE_IGNORE_NEW_LINES);
foreach ($data as $line) {
    if (ctype_alpha($line)) {   // Or whatever test you need
        $sxml->addChild('li', $line);
    }
}
echo $sxml->asXML();

Output:

  • Canada
  • Albania
  • Algeria
  • Andorra
  • Angola
我一直都在从未离去 2024-09-16 05:25:52

无意冒犯,但听起来您正在尝试重新发明轮子。除非您发现有趣的编码练习,否则为什么不使用现有的许多模板系统呢? (提示:聪明)这会让你有时间做“更重要的事情”。

No offence, but it sounds like you are trying to reinvent the wheel. Unless you find that an interesting coding exeercise, why not use of the emany templating systems out there? (hint: Smarty) That would leave your time free for "more important stuff".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文