doukang7486 2014-09-05 17:46
浏览 19
已采纳

具有格式化内容的帖子的“阅读更多”选项 - PHP

I'm using text editors for entering the posts on my WEB (this one). The text is well formated (I have images, text is styled), so the post in my database are stored in their HTML format. For example this is how a post with an image and some text will be stored in database:

<p><img alt="image" src="./img/news-img.jpg" style="height1:311px; width1:1157px" /></p>
<p style="text-align: justify;"><span style="color:rgb(112, 112, 112); font-family:opensansregular; font-size:14px">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</span></p>

And it will look something like this when is displayed. I'm using only PHP without any framework. Now I want to have only a few words on the homepage for each post and to add a button "Read More" that will lead to the page with all the content of the post. I have tried (but I expected that it won't work) to substring the text. It is not working because the post is taken as normal string (php can not see that the tags are not closed) and If I specify to take only first 200 characters and in the mean time there is <i> tag, if the closing </i> tag is not in those 200 characters, all the text below my post is in italic. Or another scenario, if is split with taking 200 characters the image is not shown because the text is not taken completely.

Is there a way that I could convert the content somehow, and when taking the first 200 charterers (this number is just an example and no need to be fixed for all the post) from the database, all the tags to be closed in the mean time and the content below that post to be normal?

  • 写回答

2条回答 默认 最新

  • duanci9305 2014-09-19 07:16
    关注

    I solved my problem using this function:

    function closetags($html) {
        #put all opened tags into an array
        preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
        $openedtags = $result[1];   #put all closed tags into an array
        preg_match_all('#</([a-z]+)>#iU', $html, $result);
        $closedtags = $result[1];
        $len_opened = count($openedtags);
    
        # all tags are closed
        if (count($closedtags) == $len_opened) {
            return $html;
        }
    
        $openedtags = array_reverse($openedtags);
        # close tags
        for ($i=0; $i < $len_opened; $i++) {
            if (!in_array($openedtags[$i], $closedtags)){
                $html .= '</'.$openedtags[$i].'>';
            } else {
                unset($closedtags[array_search($openedtags[$i], $closedtags)]);    
            }
        }  
        return 
    
    $html;
    }
    

    I found this solution here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算