Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP目录_Php_List - Fatal编程技术网

PHP目录

PHP目录,php,list,Php,List,我想制作一个内容列表,就像维基百科一样。我的代码: <?php $str = "[H1]Top Heading[/H1] My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3] string now again [H1]Top Heading[/H1] My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3]

我想制作一个内容列表,就像维基百科一样。我的代码:

<?php 
$str = "[H1]Top Heading[/H1] My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3] string now again [H1]Top Heading[/H1] My test string [H2]My sub Heading[/H2] My second [H3]My deep Heading[/H3]";

/**
 * Helper class
 */
class FaqHelper {
    static $count = 1;
    static $listItems = array();
    static $prefix = 'faq-';

    static function GetList() {

        $items = '';
            foreach (self::$listItems as $id => $label) {
                $items .= '<li><a href="#' . self::$prefix . $id .'">' . $label . '</a></li>';
            }

        return '<ul><ol>'. $items .'</ul></ol>';
    }
}

// the callback function
function make_faq($matches)
{
    $id = FaqHelper::$count;
    $label = $matches[1];

    FaqHelper::$listItems[$id] = $label;

  $res = '<span id="'. FaqHelper::$prefix . $id .'">' . $label . '</span>';

    FaqHelper::$count++;

    return $res;
}

$text = preg_replace_callback(
    "#\[H1\]([^\[]+)\[/H1\]#",
    "make_faq",
    $str
);

$list = FaqHelper::GetList();

echo $list;
echo '<br /><br />';
echo $text;


?>
但是我想要这个结果

     1. Top Heading
        1.1 My sub Heading 
            1.1.1 My deep Heading 
     2. Top Heading
        2.1 My sub Heading 
            2.1.1 My deep Heading 
等等。。。 顶部标题我的测试字符串我的副标题我的第二个我的深标题字符串现在再次顶部标题我的测试字符串我的副标题我的第二个我的深标题字符串现在再次

基本理论是这样的

当你第一次点击[H1]时,你将计数器增加到1。当你击中h2时,你的二级计数器增加到1。当你击中h3时,你的三级计数器增加到1

这显然将输出1.1.1


现在,当您点击下一个h1时,它们的子级别(即不是主级别)将重置为0,然后第一个级别将递增为2。你点击h2,它又变为1。。。然后是第一个级别2,sub 1,sub 1=2.1.1。

请注意,此代码只支持3个级别的标题。我知道这个解决方案不会无限扩展,也不支持递归解析,但不幸的是,我没有时间为如此多的问题创建完美的解决方案

下面是基于您最后一个问题的扩展解决方案。请测试它,看看它是否如预期的那样工作。请注意,我制作了与区分大小写的[H1][/H1]标记匹配的正则表达式,因此[H1][/H1]现在也可以工作

再说一次,如果我的速度慢或者结果不完美,我很抱歉,但我不能完成你的整个项目,你需要自己做一些研究,学习,然后再做。此外,随着代码的开发和增厚,进行进一步的扩展和维护将需要更多的时间,而我并不总是有能力这样做

享受吧

<?php

$str = "
[h1]Header 1[/h1]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae rutrum tellus. Vestibulum nec nisl eu enim venenatis ornare. Suspendisse a nunc nibh, quis placerat urna. Pellentesque nec odio est. [h2]Header 2[/h2]Morbi et orci non nulla lobortis convallis. Curabitur vestibulum, felis auctor posuere posuere, mauris neque vulputate tellus, vel luctus urna sapien vitae sapien. [h2]Header 2-2[/h2] Integer accumsan lobortis euismod. Integer vitae tempor nisl.

[h1]Header 1[/h1]Mauris varius dolor nec risus viverra egestas. Suspendisse potenti. Nunc consectetur faucibus metus, nec sagittis felis pellentesque non. Ut mattis ligula non [h2]Header 2[/h2] purus vehicula sed tempus risus porttitor. Nullam eget arcu[h3]Header 3[/h3] at mi pulvinar pellentesque eu sed ipsum. Fusce vel laoreet sem. Duis hendrerit ligula iaculis felis hendrerit euismod vel ut mauris. Pellentesque tempus velit et velit cursus sodales. Sed adipiscing vulputate nibh sed venenatis. Quisque sit amet ante velit. Donec dui lectus, ultricies quis interdum eget, elementum vitae nibh. Nullam nec sapien purus, laoreet porta elit. Nam luctus elit sit amet est ultricies eget varius diam ullamcorper. Sed commodo viverra lobortis. Vivamus semper blandit arcu semper pellentesque. Duis interdum tincidunt nisl eget mollis.

[h1]Header 1[/h1]Suspendisse laoreet, tortor sed viverra pellentesque, enim felis tempus ante, sit amet dignissim leo augue non ipsum. Ut eu risus erat. Donec pellentesque ligula ac est tincidunt ullamcorper. [h2]My paragraph again[/h2] Pellentesque eget diam lacus, nec ornare urna. Nulla a interdum augue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et justo turpis. ";

class FaqHeading
{
    var $heading_level;
    var $id;
    var $label;

    var $children = array();
    var $parent = null;

    function __construct($heading_level, $id, $label)
    {
        $this->heading_level = (int) $heading_level;
        $this->id = (int) $id;
        $this->label = $label;
    }

    function AddChild(FaqHeading $child)
    {
            if ($child->heading_level <= $this->heading_level) {
                return; // cannot add higher or equal leveld headings as children
            }

        $child->parent = $this;
        $this->children[] = $child;
    }

    function HasChildren()
    {
        return (bool) $this->children;
    }
}

class FaqHeadingList
{
    var $headings = array();
    var $current = null;
    var $prefix;

    function __construct($prefix)
    {
        $this->prefix = $prefix;
    }

    function AddHeading($heading_level, $id, $label)
    {
        $elem = new FaqHeading($heading_level, $id, $label);

            if ($this->current == null || $heading_level == 1) {
                $this->headings[] = $elem;
            }   else if ($heading_level == 2 && ($this->current->heading_level == 1)) {
                $this->current->AddChild($elem);
            } else if ($heading_level == 2 && ($this->current->heading_level == 2)) {
                $this->current->parent->AddChild($elem);
            } else if ($heading_level == 2 && ($this->current->heading_level == 3)) {
                $this->current->parent->parent->AddChild($elem);
            } else if ($heading_level == 3 && ($this->current->heading_level == 1)) {
                $this->current->AddChild($elem);
            } else if ($heading_level == 3 && ($this->current->heading_level == 2)) {
                $this->current->AddChild($elem);
            } else if ($heading_level == 3 && ($this->current->heading_level == 3)) {
                $this->current->parent->AddChild($elem);
            }

        $this->current = $elem;
    }

    function ToString()
    {
        $str = '<ol>';
            foreach ($this->headings as $h1)
            {
                    if ($h1->heading_level == 1)
                    {
                        $str .= '<li>'. $this->CreateLink($h1->id, $h1->label);
                            if ($h1->hasChildren())
                            {
                                $str .= '<ol>';
                                    foreach ($h1->children as $h2)
                                    {
                                        $str .= '<li>' . $this->CreateLink($h2->id, $h2->label);

                                            if ($h2->hasChildren())
                                            {
                                                $str .= '<ol>';
                                                    foreach ($h2->children as $h3)
                                                    {
                                                        $str .= '<li>'. $this->CreateLink($h3->id, $h3->label) .'</li>';
                                                    }
                                                $str .= '</ol>';
                                            }
                                        $str .= '</li>';
                                    }
                                $str .= '</ol>';
                            }
                        $str .= '</li>';
                    }
            }
        $str .= '</ol>';

        return $str;
    }

    function CreateLink($id, $label)
    {
        return '<a href="#' . $this->prefix . $id .'">' . $label . '</a>';
    }
}

/**
 * Helper class
 */
class FaqHelper
{
    static $count = 1;
    static $headingList;
    static $prefix = 'faq-';

    static function Init()
    {
        self::$headingList = new FaqHeadingList(self::$prefix);
    }

    static function ReplaceCallback($matches)
    {
        $id = self::$count;
        $heading_level = $matches[1];
        $label = $matches[2];

        self::$headingList->AddHeading($heading_level, $id, $label);

        $res = '<span id="'. self::$prefix . $id .'">' . $label . '</span>';

        self::$count++;

        return $res;
    }
}

FaqHelper::init();

$text = preg_replace_callback(
    "#\[H([0-9]+)\]([^\[]+)\[/H\\1\]#i",
    array('FaqHelper', "ReplaceCallback"),
    $str
);

$list = FaqHelper::$headingList->ToString();

echo $list;
echo '<br /><br />';
echo nl2br($text);
?>

你的具体问题是什么,你被困在哪里了?我对PHP很陌生,我不知道怎么做,请帮助我一个错误是肯定的:
return'
    $项目。
。您正在关闭
ul
之前的
ol
,即使
ol
ul
之内。好的,谢谢,但这不是答案。您是对的,我需要先学习所有这些东西,然后从现在开始项目。我将学习此感谢您的建议和代码
<?php

$str = "
[h1]Header 1[/h1]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae rutrum tellus. Vestibulum nec nisl eu enim venenatis ornare. Suspendisse a nunc nibh, quis placerat urna. Pellentesque nec odio est. [h2]Header 2[/h2]Morbi et orci non nulla lobortis convallis. Curabitur vestibulum, felis auctor posuere posuere, mauris neque vulputate tellus, vel luctus urna sapien vitae sapien. [h2]Header 2-2[/h2] Integer accumsan lobortis euismod. Integer vitae tempor nisl.

[h1]Header 1[/h1]Mauris varius dolor nec risus viverra egestas. Suspendisse potenti. Nunc consectetur faucibus metus, nec sagittis felis pellentesque non. Ut mattis ligula non [h2]Header 2[/h2] purus vehicula sed tempus risus porttitor. Nullam eget arcu[h3]Header 3[/h3] at mi pulvinar pellentesque eu sed ipsum. Fusce vel laoreet sem. Duis hendrerit ligula iaculis felis hendrerit euismod vel ut mauris. Pellentesque tempus velit et velit cursus sodales. Sed adipiscing vulputate nibh sed venenatis. Quisque sit amet ante velit. Donec dui lectus, ultricies quis interdum eget, elementum vitae nibh. Nullam nec sapien purus, laoreet porta elit. Nam luctus elit sit amet est ultricies eget varius diam ullamcorper. Sed commodo viverra lobortis. Vivamus semper blandit arcu semper pellentesque. Duis interdum tincidunt nisl eget mollis.

[h1]Header 1[/h1]Suspendisse laoreet, tortor sed viverra pellentesque, enim felis tempus ante, sit amet dignissim leo augue non ipsum. Ut eu risus erat. Donec pellentesque ligula ac est tincidunt ullamcorper. [h2]My paragraph again[/h2] Pellentesque eget diam lacus, nec ornare urna. Nulla a interdum augue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et justo turpis. ";

class FaqHeading
{
    var $heading_level;
    var $id;
    var $label;

    var $children = array();
    var $parent = null;

    function __construct($heading_level, $id, $label)
    {
        $this->heading_level = (int) $heading_level;
        $this->id = (int) $id;
        $this->label = $label;
    }

    function AddChild(FaqHeading $child)
    {
            if ($child->heading_level <= $this->heading_level) {
                return; // cannot add higher or equal leveld headings as children
            }

        $child->parent = $this;
        $this->children[] = $child;
    }

    function HasChildren()
    {
        return (bool) $this->children;
    }
}

class FaqHeadingList
{
    var $headings = array();
    var $current = null;
    var $prefix;

    function __construct($prefix)
    {
        $this->prefix = $prefix;
    }

    function AddHeading($heading_level, $id, $label)
    {
        $elem = new FaqHeading($heading_level, $id, $label);

            if ($this->current == null || $heading_level == 1) {
                $this->headings[] = $elem;
            }   else if ($heading_level == 2 && ($this->current->heading_level == 1)) {
                $this->current->AddChild($elem);
            } else if ($heading_level == 2 && ($this->current->heading_level == 2)) {
                $this->current->parent->AddChild($elem);
            } else if ($heading_level == 2 && ($this->current->heading_level == 3)) {
                $this->current->parent->parent->AddChild($elem);
            } else if ($heading_level == 3 && ($this->current->heading_level == 1)) {
                $this->current->AddChild($elem);
            } else if ($heading_level == 3 && ($this->current->heading_level == 2)) {
                $this->current->AddChild($elem);
            } else if ($heading_level == 3 && ($this->current->heading_level == 3)) {
                $this->current->parent->AddChild($elem);
            }

        $this->current = $elem;
    }

    function ToString()
    {
        $str = '<ol>';
            foreach ($this->headings as $h1)
            {
                    if ($h1->heading_level == 1)
                    {
                        $str .= '<li>'. $this->CreateLink($h1->id, $h1->label);
                            if ($h1->hasChildren())
                            {
                                $str .= '<ol>';
                                    foreach ($h1->children as $h2)
                                    {
                                        $str .= '<li>' . $this->CreateLink($h2->id, $h2->label);

                                            if ($h2->hasChildren())
                                            {
                                                $str .= '<ol>';
                                                    foreach ($h2->children as $h3)
                                                    {
                                                        $str .= '<li>'. $this->CreateLink($h3->id, $h3->label) .'</li>';
                                                    }
                                                $str .= '</ol>';
                                            }
                                        $str .= '</li>';
                                    }
                                $str .= '</ol>';
                            }
                        $str .= '</li>';
                    }
            }
        $str .= '</ol>';

        return $str;
    }

    function CreateLink($id, $label)
    {
        return '<a href="#' . $this->prefix . $id .'">' . $label . '</a>';
    }
}

/**
 * Helper class
 */
class FaqHelper
{
    static $count = 1;
    static $headingList;
    static $prefix = 'faq-';

    static function Init()
    {
        self::$headingList = new FaqHeadingList(self::$prefix);
    }

    static function ReplaceCallback($matches)
    {
        $id = self::$count;
        $heading_level = $matches[1];
        $label = $matches[2];

        self::$headingList->AddHeading($heading_level, $id, $label);

        $res = '<span id="'. self::$prefix . $id .'">' . $label . '</span>';

        self::$count++;

        return $res;
    }
}

FaqHelper::init();

$text = preg_replace_callback(
    "#\[H([0-9]+)\]([^\[]+)\[/H\\1\]#i",
    array('FaqHelper', "ReplaceCallback"),
    $str
);

$list = FaqHelper::$headingList->ToString();

echo $list;
echo '<br /><br />';
echo nl2br($text);
?>
  1. Header 1
         1. Header 2
         2. Header 2-2
   2. Header 1
         1. Header 2
               1. Header 3
   3. Header 1
         1. My paragraph again




Header 1Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam vitae rutrum tellus. Vestibulum nec nisl eu enim venenatis ornare. Suspendisse a nunc nibh, quis placerat urna. Pellentesque nec odio est. Header 2Morbi et orci non nulla lobortis convallis. Curabitur vestibulum, felis auctor posuere posuere, mauris neque vulputate tellus, vel luctus urna sapien vitae sapien. Header 2-2 Integer accumsan lobortis euismod. Integer vitae tempor nisl.

Header 1Mauris varius dolor nec risus viverra egestas. Suspendisse potenti. Nunc consectetur faucibus metus, nec sagittis felis pellentesque non. Ut mattis ligula non Header 2 purus vehicula sed tempus risus porttitor. Nullam eget arcuHeader 3 at mi pulvinar pellentesque eu sed ipsum. Fusce vel laoreet sem. Duis hendrerit ligula iaculis felis hendrerit euismod vel ut mauris. Pellentesque tempus velit et velit cursus sodales. Sed adipiscing vulputate nibh sed venenatis. Quisque sit amet ante velit. Donec dui lectus, ultricies quis interdum eget, elementum vitae nibh. Nullam nec sapien purus, laoreet porta elit. Nam luctus elit sit amet est ultricies eget varius diam ullamcorper. Sed commodo viverra lobortis. Vivamus semper blandit arcu semper pellentesque. Duis interdum tincidunt nisl eget mollis.

Header 1Suspendisse laoreet, tortor sed viverra pellentesque, enim felis tempus ante, sit amet dignissim leo augue non ipsum. Ut eu risus erat. Donec pellentesque ligula ac est tincidunt ullamcorper. My paragraph again Pellentesque eget diam lacus, nec ornare urna. Nulla a interdum augue. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et justo turpis.