我需要动态地将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)
黑色喜剧,由Alice Lowe主演
图片/观光者-蒂娜铅笔.jpg
有谁知道可以读取第 X 行(或本例中的第 3 行)并动态填充 img src=" 文件路径的 php 代码?
提前感谢您的建议!
如果您需要遍历行并需要检查行是否包含文件路径(例如,如果有时块之间有白线,有时它们丢失),则:
<?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
global var $raw;
$myFile = "film_music/feature1.txt";
// open file...
$lines = file($myFile);//file in to an array<br />
for($i=2;$i<=no_of_lines;$i+3){
$raw = $lines[$i];
}
// Populate $raw variable where you need.
?>
使用$fh = fopen(...)
并循环每一行。
while (!feof($fh)) {
$line = fgets($fh);
if ($line === false) {
throw new Exception("File read error");
}
[do your things.]
}
http://www.php.net/manual/en/function.fopen.php
您可以将文本行的每一行读取为:
$lines=file('file.txt');
$lines=array();
$fp=fopen('file.txt', 'r');
while (!feof($fp))
{
$line=fgets($fp);
//process line however you like
$line=trim($line);
//add to array
$lines[]=$line;
}
fclose($fp);
如果您不需要任何特殊处理,这应该可以满足您的需求
$lines = file($lines, FILE_IGNORE_NEW_LINES);
愿这对你有所帮助... :)
您可以根据需要使用in_array("you want", $lines)
检查所有文本文件行...如果不是动态的....