为用户视图创建内容格式

时间:2017-08-30 01:58:27

标签: php preg-replace str-replace

我想制作自己的格式内容,我已经完成了段落和列表,但是有些问题我无法解决。这就是问题所在:

我无法在群组中获得第一个 [li] 和最后一个 [li] , 例如:

some text
more text here
[li]......[/li] /*
[li]......[/li]  * here is one group that will be wrapped with <ul></ul>
[li]......[/li]  *
there are some text here as well
[li]......[/li] /* this will be wrapped with <ul></ul> too
[li]......[/li]  * 

以下是我想要的条件:

  1. 如果在内容中找到 [li] ,那么它将用 ul括号覆盖它,然后将所有 [li] 替换为 li tags
  2. 所有 \ n 将替换为 [br]
  3. [br] 会爆炸,知道什么是段落,列表组是什么
  4. 这是我存储在数据库中的内容

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed blandit molestie lacus id consequat. Cras varius tincidunt fermentum. Morbi bibendum risus nec ligula ultrices pretium. Sed bibendum ultrices sem, interdum dignissim risus imperdiet eget. Cras viverra est risus. Donec pretium commodo dui, eu vehicula massa sollicitudin quis. Praesent sit amet purus libero. Morbi dignissim metus nec convallis posuere. Pellentesque enim tellus, efficitur sed volutpat at, pellentesque in urna. Nam accumsan laoreet tellus, sed dapibus urna congue nec. Mauris pharetra dui lorem. Aenean imperdiet nibh erat, sed vulputate ex aliquam sed. Sed eu eros non lorem ornare faucibus sit amet id elit.
    [li]Sunrise and mountain view early in the morning[/li]
    [li]Breakfast in the in rice field[/li]
    [li]Explore the Yeh Mataan river and the water[/li]
    [li]Lunch and Napping with the beach view[/li]
    [li]Enjoy the beach and witness the sunset[/li]
    Donec auctor ipsum ipsum, vitae rutrum tellus porttitor vitae. Praesent sodales sollicitudin mauris ac viverra. Aenean sodales odio ac volutpat mattis. Nunc quis sapien ut nisi ultrices consectetur.
    

    这是到目前为止的代码

        private $html_format = array(
                        "\n[li]" => '<li>',
                        "\n"     => '[br]',
                        '[li]'   => '<li>',
                        '[/li]'  => '</li>',
                        '[b]'    => '<b>',
                        '[/b]'   => '</b>'
                );
    
    
        public function description_decoder($data) 
        {
                foreach ($this->html_format AS $format_key => $format_value) {
                        $data = str_replace($format_key, $format_value, $data);
                }
    
                $exploded_data = explode('[br]', $data);
                $decoded_data  = '';
                print_r($exploded_data);
    
                $list = 0;
    
                foreach ($exploded_data AS $paragraph) {
                        if (!empty($paragraph)) {
                                if (strpos($paragraph, '</li>')) {
                                        if ($list == 0) $paragraph = '<ul>' . $paragraph;
                                        elseif ($list == 1) $paragraph = $paragraph . '</ul>';
    
                                }
                                else $paragraph = '<p>' . $paragraph . '</p>';
    
                                $decoded_data .= $paragraph;
                        }
                }
    
    
                return $decoded_data;
        }
    

    请帮助,我不知道如何实现这个

0 个答案:

没有答案