PHP 代码输入数据 {a: Item1|item2|item3 } 以格式化为 HTML 列表


PHP code input data {a: Item1|item2|item3 } to Format into HTML list

我正在开发一个脚本来格式化数据:

如果需要,请更换它们(您的医生会帮助您):{1:您的椅子|您的枕头|{I:你的椅子1|您的枕头 2|您的床垫 3|你的鞋子 4}|你的鞋子},我希望 {} 中的所有内容都作为新列表,并在大括号 {a: } 内说明类型

首选分隔符是 |

PHP代码 :

function processforsublist($str)
{
    $start = strpos($str, '{');
    $format = substr($str, $start+1 , 1);
    $end = strpos($str, '}');
    if($start!==false && $end!==false)
    {
    $cut = substr($str, $start, ($end-$start)+1);
    $class  = '';
    switch ($format) {
        case '1':
            $class = 'list_number';
            break;
        case 'a':
            $class = 'list_alpha_small';
            break;
        case 'A':
            $class = 'list_alpha_caps';
            break;
        case 'i':
            $class = 'list_roman_small';
            break;
        case 'I':
            $class = 'list_roman_caps';
            break;
        default:
            $class = 'list_number';
            break;
    }
    $cutn = str_replace('{', '', $cut);
    $cutn = str_replace('}', '', $cutn);
    $cutn = str_replace($format.":", '', $cutn);
    //echo $cutn;
    $exploded = explode('|', $cutn);
    $lis = '';
    foreach ($exploded as $value) {
        if(strpos($str, '{')!==false)
        $lis .= "<li>".processforsublist($value)."</li>";
        else
        $lis .= "<li>".trim($value)."</li>";    
    }

    $newstr = "<ol class='$class'>$lis</ol>";
    echo $str."<hr/>".$cut."<hr/>".$newstr."<hr/>";
    $istr = str_replace($cut, $newstr, $str);   

    return $istr;
    }
    else {
        return $str;
    }       
}
echo processforsublist('Change them if needed (your doctor will help you with this):{1:Your chair|Your pillow|{I:Your chair 1|Your pillow 2|Your mattress 3|Your shoes 4}|Your shoes}');

电流输出 :

Change them if needed (your doctor will help you with this):
    1.Your chair
    2.Your pillow
    3.Your chair 1
    4.Your pillow 2
    5.Your mattress 3
    6.Your shoes 4
|Your shoes}

所需输出:

Change them if needed (your doctor will help you with this):
    1.Your chair
    2.Your pillow
        I.Your chair 1
        II.Your pillow 2
        III.Your mattress 3
        IV.Your shoes 4
    3.Your shoes

你在那里做了太多的工作。以下似乎足以满足您的要求。

$str="{1:Your chair|Your pillow|{I:Your chair 1|Your pillow 2|Your mattress 3|Your shoes 4}|Your shoes}";
$str=str_replace("{1:","<ol type=1><li>",$str);
$str=str_replace("{a:","<ol type=a><li>",$str);
$str=str_replace("{A:","<ol type=A><li>",$str);
$str=str_replace("{i:","<ol type=i><li>",$str);
$str=str_replace("{I:","<ol type=I><li>",$str);
$str=str_replace("}","</li></ol>",$str);
$str=str_replace("|","</li><li>",$str);

从您的描述来看,听起来 JSON 字符串可能是一个更好的选择。看看json_encodejson_decode.它会让你的生活好一百万倍。

我认为有几件事你需要改变。这里只是简要介绍一下:

  1. 尝试$end = strrpos($str, '}');代替$end = strpos($str, '}');,因为在当前代码中,它会搜索 } 的第一个出现,因此您拥有的字符串将被搜索为 { 的第一次出现和 } 的第一次出现,这是错误的,因为它之间会有任意数量的"{"。

  2. $cutn = str_replace('{', '', $cut); $cutn = str_replace('}', '', $cutn); 这将替换所有出现的

试试$cutn = preg_replace('/{/', '', $cut,1); $cutn =substr_replace($cutn,'', strrpos($cutn, '}'), 1);

  1. $exploded = explode('|', $cutn);,这将为您提供所有内容的数组,但不是子数组,因为您需要它来显示罗马数字。 所以你需要改变它。

希望这有帮助