检查字符串并根据输入创建页面结构


PHP - Check a string and create a page structure from input

我试图为页面结构系统编写一个脚本,理想情况下,用户以某种方式进入页面结构,然后脚本以稍微不同的方式输出。这很难解释,但我想要的是这个;

= Section 1

第1页

第2页

= Section 2

第1页

第2页

将变成以下内容;

Section 1' Section -1||Page 1

Section 1' Section -1||Page 2

Section 2' Section -2||Page 1

Section 2' Section -2||Page 2

所以基本上输入被格式化成一个网站结构,可以插入到另一个系统,可以为一个网站生成页面。说来话长,但这是如何实现的呢?我对PHP并不陌生,但仍然处于初级/初学者的水平。

所以每个"节"是一个类别,当然每个页面…是一个页面。请帮忙好吗?

这个小解析器将做:

$sites = array();
// Split input by new lines.
foreach(preg_split( '/'r'n|'r|'n/', $input) as $line) {
    // Skip empty lines
    if(!trim($line)) {
        continue;
    }
    // Check for a section header (starts with a `=<space>`)
    if(strpos($line, '=') === 0) {
        $current = substr($line, 2); 
        $current .= '''' . str_replace(' ', '-', strtolower($current));
    } else {
        // Build site paths and add to the array
        $sites []= $current . "||$line";
    }
}
echo implode(PHP_EOL, $sites);
输出:

Section 1'section-1||Page 1
Section 1'section-1||Page 2
Section 2'section-2||Page 1
Section 2'section-2||Page 2