简体   繁体   English

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

[英]Formatting PHP outputted plain text into HTML mark-up

We have a client who wants to output cinema listings on their website. 我们有一个客户想要在其网站上输出电影列表。 This particular cinema has provided us with a link to retrieve the information from. 这个特殊的电影院为我们提供了一个从中检索信息的链接。 This link is simply outputted in plain text, with the elements seperated by ~|~ 该链接仅以纯文本形式输出,其元素用〜|〜分隔。

I have set up a curl script to grab this plain text from the url and display it as plain text on the clients website. 我已经设置了一个curl脚本来从URL中获取此纯文本并将其显示为客户网站上的纯文本。 However I now need a way of pulling this information into div classes so I can style it with CSS. 但是,我现在需要一种将该信息提取到div类中的方法,以便可以使用CSS设置样式。 Also there are a few links which I need to format into buttons that link to book the show. 还有一些链接,我需要将其格式化为链接以预订节目的按钮。

This is my current code: 这是我当前的代码:

<?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>"

?>

All this is doing currently is replacing the separators with a space. 当前所做的全部工作就是用空格替换分隔符。 I'm really confused on this one and I'm not 100% proficient with PHP. 我对此很困惑,而且我不是100%精通PHP。 Any ideas? 有任何想法吗?

Thanks in advance 提前致谢

Try this 尝试这个

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

As you said in your description, your function is just replacing the character with a space. 正如您在描述中所说的那样,您的功能只是将字符替换为空格。

What you need is split the file and separate in different arrays depending from the format. 您需要的是将文件拆分并根据格式分成不同的数组。

to make an array you use the function explode 要创建一个数组,请使用函数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>";
        }}

I have given you an example, but I dont know what the file includes and I cant give you a good validation method... If it is a nice table, than you can format it quite esily using the index number.. 我给了你一个例子,但是我不知道文件包含什么,也不能给你一个很好的验证方法...如果它是一个不错的表,那么你可以很容易地使用索引号格式化它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM