2016-02-14 41 views
-1

您好,我需要从文本文件中提取一些图像链接。 它应该存储在一些变量中,以便链接可以重复使用。 我只需要在img文件与image1.jpg结束PHP读取字符串,直到文本文件中

我用这个代码

<?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&#235;n C3</b>&nbsp;1.4i Diff&#233;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'> 
+0

当前代码会发生什么?另外,最后一个函数应该是'fclose',除非你写了'close'函数.. – chris85

+0

嗨,你是对的,这是一个错字,当前的代码不工作,因为它不被视为1个文本字符串,每行都有缺少信息,而不仅仅是在文件的开头。 –

回答

0

做这样的事情

$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); 
    } 
} 
+0

谢谢我明天会试试这个,我想我认为这是我需要的!非常感谢! –

+0

你在de文件auto.inc中改变了一些东西。现在我必须跳过第一个图像链接,我想要第二个链接。你能再次帮我吗?这是我想从中提取第二个链接的代码示例。 :

相关问题