PHP从文本文件中读取字符串直到
嗨,我需要从文本文件中提取一些图像链接。 它应该存储在一些变量中,这样链接就可以重用。 我只需要以image1.jpg结尾的img文件 我用了这个密码PHP从文本文件中读取字符串直到,php,html,Php,Html,嗨,我需要从文本文件中提取一些图像链接。 它应该存储在一些变量中,这样链接就可以重用。 我只需要以image1.jpg结尾的img文件 我用了这个密码 <?php $myfile = fopen("vwe/autos.inc", "r") or die("Unable to open file!"); // Output one line until end-of-file while(!feof($myfile)) { $photo1 = fg
<?php
$myfile = fopen("vwe/autos.inc", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
$photo1 = fgets($myfile);
//echo var_export(substr($photo1, 0, 30))."<br>";
echo "1)".substr($photo1, 10, 30)."<br>";
echo "2)".substr($photo1, 40, 80)."<br>";
}
close($myfile);
?>
Autos.inc看起来像这样:
<div class='glidecontent'>
<div id='positiontest'>
<a href='occasion.aspx' target='_self'>
<img src='http://site.nl/643607012/image1.jpghttp://sit.nl/643607013/image2.jpghttp://site.nl/643607014/image3.jpg' width='100%' border='0' height='100%' style='float: top; padding: 0px' />
</a>
<div id='textonadd'>
<b>Citroën C3</b> 1.4i Différence</br>
Bouwjaar: 2004 | Prijs: 3250 euro
</div>
</div>
</div>
<div class='glidecontent'>
<div id='positiontest'>
<a href='occasion.aspx' target='_self'>
<img src='http://site.nl/643587726/image1.jpghttp://site.nl/643587727/image2.jpghttp://site.nl/643587728/image3' width='100%' border='0' height='100%' style='float: top; padding: 0px' />
</a>
<div id='textonadd'>
雪铁龙ë;n C3 1.4i差异和#233;法国
Bouwjaar:2004 | Prijs:3250欧元
像这样做
$data = file_get_contents("vwe/autos.inc", "r") or die("Unable to open file!");
$images = array();
preg_match_all('/(img|src)=("|\')[^"\'>]+/i', $data, $media);
unset($data);
$data = preg_replace('/(img|src)("|\'|="|=\')(.*)/i', "$3", $media[0]);
foreach ($data as $url) {
$info = pathinfo($url);
if (isset($info['extension'])) {
if (($info['extension'] == 'jpg') ||
($info['extension'] == 'jpeg') ||
($info['extension'] == 'gif') ||
($info['extension'] == 'png'))
array_push($images, $url);
}
}
您当前的代码会发生什么情况?另外,最后一个函数应该是
fclose
,除非您编写了close
函数。您好,您是对的,这是一个打字错误,当前代码不起作用,因为它不被视为一个文本字符串,并且每行都缺少信息,而不是仅在文件的开始处。谢谢,我明天将尝试这个,我想这是我需要的!非常感谢!嗨,de file auto.inc中发生了一些变化。现在我必须跳过第一个图像链接,我想要第二个链接。你能再帮我一次吗?这是我想从中提取第二个链接的代码示例: