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代码输入数据{a:Item1 | item2 | item3}以格式化为HTML列表_Php_List_Explode - Fatal编程技术网

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

PHP代码输入数据{a:Item1 | item2 | item3}以格式化为HTML列表,php,list,explode,Php,List,Explode,我正在开发一个脚本来格式化数据: 如果需要,可以更换(你的医生会帮你):{1:你的椅子|你的枕头{I:你的椅子1 |你的枕头2 |你的床垫3 |你的鞋4}你的鞋}我希望{}里面的所有东西都像新的清单一样,在支架内注明类型{a:} 首选分离器为| PHP代码: function processforsublist($str) { $start = strpos($str, '{'); $format = substr($str, $start+1 , 1); $end =

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

如果需要,可以更换(你的医生会帮你):{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
        I.Your chair 1
        II.Your pillow 2
        III.Your mattress 3
        IV.Your shoes 4
    3.Your shoes

根据您的描述,听起来JSON字符串可能是一个更好的选择。查看
json\u编码
json\u解码
。这会让你的生活好上百万倍。

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

  • 试试
    $end=strrpos($str,'}')
    代替
    $end=strpos($str,'}')
    与当前代码一样,它搜索第一次出现的},因此您拥有的字符串将作为第一次出现的{和第一次出现的}进行搜索,这是错误的,因为它将有任意数量的{介于两者之间

  • $cutn=str_replace('{','$cut);
    $cutn=str_replace('}','$cutn);
    这将替换所有发生的事件

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

  • $explode=explode(“|”,$cutn);
    这将为您提供所有内容的数组,但不提供显示罗马数字所需的子数组。因此您需要更改它

  • 希望这有帮助,你在那里做了太多的工作。 以下内容似乎足以满足您的需求

    $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);
    
    $str=“{1:你的椅子|你的枕头|{I:你的椅子1 |你的枕头2 |你的床垫3 |你的鞋4 |你的鞋}”;
    $str=str_替换(“{1:”,“
  • ”,$str); $str=str_替换(“{a:”,“
  • ”,$str); $str=str_替换(“{A:”,“
  • ”,$str); $str=str_替换(“{i:”,“
  • ”,$str); $str=str_替换(“{I:”,“
  • ”,$str); $str=str_替换(“}”,“
  • ”,$str); $str=str_替换(“|”和“
  • ”,$str);
  • 好的。问题出在哪里?嗯……解决办法是写一些代码。你试过雇佣开发人员吗?因为你有项目要求。谢谢你……这帮了大忙……)
    $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);