php - 我可以使用 PHP 在网页中添加 .txt 文件中的图像路径吗?

标签 php html text

我需要动态地将 img src 完整文件路径添加到 .txt 文件中的文件。到目前为止,我一直在使用以下代码来填充标题和描述:

< ?php

    $myFile = "film_music/feature1.txt";
$lines = file($myFile);//file in to an array<br />
echo $lines[0]; //line 1

    ? >

功能文件包含最终将在此处显示的电影的所有详细信息:

http://www.londonosophy.com/film_music2.php

当前该文件包含以下行:

观光客 (2012)
黑暗喜剧,由爱丽丝·洛主演
图片/Sightseers-TinaPencil.jpg

有人知道可以读取第 X 行(或本例中的第 3 行)并动态填充 img src=""文件路径的 php 代码吗?

非常感谢您的建议!

最佳答案

如果您需要迭代行并需要检查行是否包含文件路径(例如,如果有时 block 之间有白线,有时它们丢失),则:

<?php
$myFile = 'film_music/feature1.txt';
$lines = file($myFile);
$needle = 'images/';
$needleLen = strlen($needle);
foreach ($lines AS $line) {
  $line = trim($line);
  if (substr($line, 0, $needleLen) == $needle) {
    echo '<img src="' . $line . '" alt="" />';
  }
}
?>

关于php - 我可以使用 PHP 在网页中添加 .txt 文件中的图像路径吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18828310/

相关文章:

PHP、Mysql 从 strtotime 按月获取存档

php - Nginx 从不同的服务器提供 php 文件

php - 在这些条件下发生了什么?

php - 如何在 Doctrine2 中为类表继承指定外键列?

Python Tkinter 文本背景

text - 使用@font-face 时,SVG 文本边界框因浏览器而异?

android - 如何根据加载的文本增加我的 android textview 中的文本大小?

javascript - 为什么我的自定义模块在 YUILoader.onSuccess 事件触发后不可用?

jquery - 使用 CSS 打印 jQuery 对话框

javascript - 在 title 属性中添加按钮链接 - HTML