2013-07-22 93 views
0

我们有一个客户希望在其网站上输出电影列表。这个特殊的电影为我们提供了一个链接来检索信息。此链接以纯文本格式输出,元素之间用〜|将PHP输出的纯文本格式化为HTML标记

我已经设置了一个curl脚本来从url中获取这个纯文本并在客户端网站上以纯文本形式显示它。不过,我现在需要一种将这些信息拉入div类的方式,以便我可以使用CSS进行设计。此外还有几个链接,我需要将它们格式化为链接以预订节目的按钮。

这是我当前的代码:

<?php 

function curl_get_file_contents($URL) 
    { 
     $c = curl_init(); 
     curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($c, CURLOPT_URL, $URL); 
     $uf_contents = curl_exec($c); 
     curl_close($c); 

     $contents = str_replace("~|~"," ",$uf_contents); 

     if ($contents) return $contents; 
      else return FALSE; 
    } 

echo "<ul>"; 
echo curl_get_file_contents("THE URL"); 
echo "</ul>" 

?> 

这一切目前正在做的是用空格替换分隔符。我对这个很困惑,而且我不是100%熟练使用PHP。有任何想法吗?

在此先感谢

+2

循环浏览从curl获得的内容并添加标签有什么问题? – Anigel

+0

'〜|〜'是一个奇怪的分隔符:\ –

回答

4

试试这个

foreach (explode("~|~", file_get_contents("URL")) as $item) 
{ 
    echo '<div>' . $item . '</div>'; 
} 
0

正如你说你的描述,你的功能仅仅是更换一个空格字符。

你需要的是将文件拆分并根据格式分开放入不同的数组中。

让你使用功能的阵列爆炸

//this will split the content in an array 
    $theArray = explode("~|~"," ",$uf_contents); 

    //here you can assign a different class depending from the value (this just if you 
    can distinguish between the various variable quite easily.. For example 
    a website because of the .com a rating because of the number). Below there are 
    example of website, rating. 

    foreach($theArray as $item){ 
     if(strpos($item,'.com') === true){ 
      echo "<div class='webSite'>" . $item . "</div>"; 
     }elseif($item >=0 && $item <= 100){ 
      echo "<div class='webSite'>" . $item . "</div>"; 
     }else{ 
      echo "<div class='normalDiv'>" . $item . "</div>"; 
     }} 

我给你一个例子,但我不知道是什么文件,包括我不能给你一个很好的验证方法...如果它是一个不错的表格,比你可以使用索引号格式非常容易地格式化。