PHP - 字符串的操作

时间:2016-07-21 09:29:07

标签: php string

这是原始数据:

3
1/1.0
Question
Answer 1
Answer 2
....
Answer n
2
1/2.0
Question
Answer 1
Answer 2
....
Answer n

这是所需的输出:

[ 
     3, 1.0, [],            
    "Question",
    [
        "Answer 1",
        "Answer 2",
        ....
        "Answer n"          
    ]               
],
[ 
     2, 2.0, [],            
    "Question",
    [
        "Answer 1",
        "Answer 2",
        ....
        "Answer n"          
    ]               
],

这是我到目前为止所做的:

$step1 = str_replace("1/"," ", $raw);
$step2 = explode(PHP_EOL, $step1);
foreach ($step2 as $key => &$value) {   
    if(floatval($value) == 0) {
         $value = '"'.$value.'"';
    };
    $value = $value.",";
}

现在棘手的部分:如何添加括号和标签。 我整天都在敲头。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<div class="container">
  <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
    <div class="panel panel-info">
        <div class="panel-heading " role="tab" id="headingOne">
            <h4 class="panel-title">
                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#1" aria-expanded="true" aria-controls="collapseOne">
                1
                </a>
            </h4>
        </div>
        <div id="1" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
            <div class="panel-body">
                Panel body data
            </div>
        </div>
    </div>
    <div class="panel panel-info">
        <div class="panel-heading" role="tab" id="headingTwo">
            <h4 class="panel-title">
                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#2" aria-expanded="false" aria-controls="collapseTwo">
                    2
                </a>
            </h4>
        </div>
        <div id="2" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
            <div class="panel-body">
                Panel body data
            </div>
        </div>
    </div>
    <div class="panel panel-info">
        <div class="panel-heading" role="tab" id="headingThree">
            <h4 class="panel-title">
                <a class="collapsed" role="button" data-toggle="collapse" data-parent="#accordion" href="#3" aria-expanded="false" aria-controls="collapseThree">
                  3
                </a>
            </h4>
        </div>
        <div id="3" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
            <div class="panel-body">
                Panel body data
            </div>
        </div>
    </div>
</div>
</div>

输出

$raw = "3
1/1.0
Question
Answer 1
Answer 2
....
Answer n
2
1/2.0
Question
Answer 1
Answer 2
....
Answer n";

$getAllLines = array_map("trim", explode("\n", $raw));
$endStr = "";
$nextIsQuestion = false;

foreach ($getAllLines as $getLine) {
    if (is_numeric($getLine)) {
        if (!empty($endStr)) {
            $endStr = substr($endStr, 0, -2) . "\n";
            $endStr .= "\t]\n],\n";
        }
        $endStr .= "[\n\t" . $getLine . ", ";
    }   
    else {
        if (strpos($getLine, "/") !== false) {
            $getNum = substr($getLine, strpos($getLine, "/") + 1);
            $endStr .= $getNum . ", [],\n\t";
            $nextIsQuestion = true;
        }
        elseif ($nextIsQuestion) {
            $endStr .= "\"" . $getLine . "\",\n\t[\n";
            $nextIsQuestion = false;
        }
        else {
            $endStr .= "\t\t\"" . $getLine . "\",\n";
        }
    }
}
$endStr = substr($endStr, 0, -2) . "\n";
$endStr .= "\t]\n]";

echo $endStr;