简体   繁体   English

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

[英]“Read More” option for posts with formatted content - PHP

I'm using text editors for entering the posts on my WEB ( this one ). 我正在使用文本编辑器在我的WEB( )上输入帖子。 The text is well formated (I have images, text is styled), so the post in my database are stored in their HTML format. 文本格式正确(我有图像,文本带有样式),因此数据库中的帖子以HTML格式存储。 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. 我只使用PHP,没有任何框架。 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. 它不起作用,因为该帖子被当作普通字符串使用(php无法看到标记未关闭),并且如果我指定仅使用前200个字符,并且在此期间有<i>标记,如果关闭</i>标记不是用这200个字符组成,我帖子下方的所有文本均用斜体显示。 Or another scenario, if is split with taking 200 characters the image is not shown because the text is not taken completely. 或另一种情况,如果拆分为200个字符,则由于文本未完全获取而无法显示图像。

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? 有没有办法我可以以某种方式转换内容,并且从数据库中获取前200个租船人(这个数字只是一个例子,不需要为所有职位固定),所有标签均应关闭时间和帖子下面的内容是否正常?

This is difficult because data structure is lacking. 这很困难,因为缺少数据结构。 Would removing the tags be an option? 是否可以选择删除标签? Is there a chance that you could add a little bit of structure? 您是否有可能添加一些结构? Say an extra field where people would store a title and you can use that as your anchor. 说一个额外的字段,人们可以在其中存储标题,您可以将其用作锚点。

Maybe a combination of both. 也许两者兼而有之。 You have a second input that you pre-fill with what you think would be a good anchor and they can improve it if there are problems. 您还有第二个输入,您可以用自己认为合适的锚点进行预填充,如果有问题,他们可以改进它。

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 . 我在这里找到了这个解决方案。

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

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