duanbing8817
2014-12-03 03:23
浏览 56
已采纳

PHP解析RSS源

I am going to a url, which is just an rss feed, and now I would like to parse the code to return certain bits of information (image urls). I've gotten as far as finding where my information starts but I can not figure out how to read until my delimiting character; which is a ". I would ultimately like to the image urls saved to a text file that my iphone app can reference.

Basically, I want to go to the url.. scan the html code for the image urls that are posted, and return it to a text file.

Here is my current code

     <?php

    $url = 'http://www.actionsportsinc.com/p200285048/recent.rss';

$data = file_get_contents( $url );

// just a test to compare with RSS to see whats being pulled
$file = 'iphoneApp.txt';

// Write the contents back to the file
file_put_contents($file, $data);



if(strpos($data, '<media:content url="') !== FALSE)
{

    $url = "";
    while (! feof($file) && (fgetc($file) != '"')) {
        $url = $url . fgetc($file);
    }

        echo $url; // just trying to print out one url right now to make sure it is actually working... would like to scan entire rss until EOF

}


?>

Here is an example of the rss looks like

<media:content url="http://www.actionsportsinc.com/img/s12/v173/p468059272-4.jpg" type="image/jpeg" medium="image" width="800" height="533"/>
<media:title>D1411HMSF134189JRN</media:title>

I would appreciate any suggestions you all may have. Thank you!

图片转代码服务由CSDN问答提供 功能建议

我要去一个url,这只是一个rss feed,现在我想将代码解析为 返回某些信息(图片网址)。 我已经找到了我的信息开始的地方但我无法弄清楚如何阅读直到我的划界角色; 这是一个“。我最终会喜欢将图片网址保存到我的iphone应用程序可以引用的文本文件中。

基本上,我想去网址..扫描html 发布的图片网址的代码,并将其返回到文本文件。

这是我当前的代码

 &lt;?  php 
 
 $ url ='http://www.actionsportsinc.com/p200285048/recent.rss';
nnnata = file_get_contents($ url); 
 
 //只是一个比较的测试 用RSS查看什么是拉动
 $ file ='iphoneApp.txt'; 
 
 //将内容写回文件
file_put_contents($ file,$ data); 
 
 
 
if(  strpos($ data,'&lt; media:content url =“')!== FALSE)
 {
 
 $ url =”“; 
 while(!feof($ file)&amp;&amp;(fgetc  ($ file)!='“')){
 $ url = $ url.fgetc($ file); 
} 
 
 echo $ url; //只是想立即打印出一个网址来制作 确定它实际上正在工作...想要扫描整个rs直到EOF 
 
} 
 
 
?&gt; 
   
 
 

这是一个 rss的例子看起来像

 &lt; media:content url =“http://www.actionsportsinc.com/img/s12/v173/p468059272-4.jpg”type =“image / jpeg”medium  =“image”width =“800”height =“533”/&gt; 
&lt; media:title&gt; D1411HMSF134189JRN&lt; / media:title&gt; 
   
 
 

我会 感谢你们所有人的建议。 谢谢!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dounangshen6553 2014-12-03 03:59
    已采纳
    $pos = 0
    while($x = strpos($data, '<media:content url="', $pos) !== FALSE)
    {
        $y = strpos($data, '" type="', $pos)
        $imgurl = substr($data, $x+'num of chars in <media:content url="', $y); 
        $pos = $x
        //write imgurl to whatever file you like
    }
    

    untested code I am sure there are syntax issues with it but should give you want you need to do it if you do not want to use an xml parser thats already been made

    what that is going for is iterating through $data starting at $pos if it finds an instance of

    Once it has both those positions itll substr and you need to pull from $data $x(start position of media:content)+ number of chars in your search param and $y start position of the end of the url...

    edit- apparently it do didnt like the $x in there like that so

    $url = 'http://www.actionsportsinc.com/p200285048/recent.rss';
    
    $data = file_get_contents( $url );
    $pos = 0;
    while(strpos($data, '<media:content url="', $pos) !== FALSE)
    {
        $x = strpos($data, '<media:content url="', $pos) ;
        $y = strpos($data, ' type="image', $x);
        $imgurl = substr($data, $x+20, $y - 49 - $x); 
        $pos = $x + 1;
        echo $imgurl . "|| <br>";
        //write imgurl to whatever file you like
    }
    

    That is the exact code pasted straight from my npp

    打赏 评论

相关推荐 更多相似问题