-4

I am developing a script to format the data :

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} and i want everything inside {} to be as new list with the type stated inside braces {a: }

prefered seperator is |

PHP Code :

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}');

Current Output :

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}

Required Output:

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
4

3 回答 3

1

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

$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);
于 2013-09-13T16:39:38.297 回答
0

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

  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);这将为您提供所有内容的数组,但不是子数组,因为您需要它来显示罗马数字。所以你需要改变它。

希望这可以帮助

于 2013-09-13T15:45:31.587 回答
0

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

于 2013-09-13T14:25:50.907 回答