将PHP输出的纯文本格式化为HTML标记

时间:2013-07-22 10:44:18

标签: php html curl

我们有一位客户希望在其网站上输出影院列表。这个特殊的电影为我们提供了从中检索信息的链接。此链接仅以纯文本格式输出,元素由〜|〜

分隔

我已经设置了一个curl脚本来从网址中获取此纯文本,并在客户端网站上将其显示为纯文本。但是我现在需要一种方法将这些信息提取到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 个答案:

答案 0 :(得分:4)

试试这个

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

答案 1 :(得分: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>";
        }}

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

相关问题