【问题标题】:Formatting PHP outputted plain text into HTML mark-up将 PHP 输出的纯文本格式化为 HTML 标记
【发布时间】:2013-07-22 10:44:17
【问题描述】:

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

分隔

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

提前致谢

【问题讨论】:

  • 循环浏览从 curl 获得的内容并添加标签有什么问题?
  • ~|~ 是一个奇怪的分隔符:\

标签: php html curl


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

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

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

    使用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>";
            }}
    

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

    【讨论】:

      猜你喜欢
      • 2012-09-05
      • 2011-03-27
      • 1970-01-01
      • 2014-04-21
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      相关资源
      最近更新 更多