0

我们有一个客户想在他们的网站上输出电影列表。这个特定的电影院为我们提供了一个链接,可以从中检索信息。该链接以纯文本形式简单输出,元素以~|~ 分隔

我已经设置了一个 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。有任何想法吗?

提前致谢

4

2 回答 2

4

尝试这个

foreach (explode("~|~", file_get_contents("URL")) as $item)
{
    echo '<div>' . $item . '</div>';
}
于 2013-07-22T10:48:56.407 回答
0

正如您在描述中所说,您的功能只是用空格替换字符。

您需要的是拆分文件并根据格式分成不同的数组。

要制作一个数组,您可以使用函数 explode

    //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>";
        }}

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

于 2013-07-22T11:09:52.863 回答