dsbckxk165039 2018-02-04 16:55
浏览 56
已采纳

PHP - 回显数组中标记的打开和关闭

Here I have an array and there is content situated inside it, their is either, one object, two, or more - depending on the tags required; the first element nested in the multidimensional array would be the textual output, unless it is an array, then the first element inside the array would be the text.

However, the other content in the array is in reference to the HTML tag they correspond to, such as:

    [1] => Array
    (
        [0] => bo          <====== Text to output
        [1] => bold        <====== tag to be within
    )

However, in the module of simplicity, I would prefer for the content not to constantly repeat such responses, like:

This is a test <b>bo</b><i><b>ld</b></i><i>,</i> <u><i>u</i></u><u>nderline</u> ...

Instead the output should be:

This is a test<b>bo<i>ld</i></b><i>, <u>u</u></i><u>nderline</u> ...

This is the PHP code I have for it so far...

$use = array();
$base = "";
foreach ($build as $part => $data) {
//  print_r($use);
    if(!is_array($data)){
        $base .= $data;
    } else {
        $text = array_shift($data);
        if(!is_array($data[0])){
            $data = array($data[0]);
        } else {
            $data = $data[0];
        }
        $removed = array_diff($use,$data);

        foreach (($data) as $tag) {
            if (in_array($tag, array_diff($use,$data))) {
                $base .= "<\/" . $tag . ">";
            } elseif(!in_array($tag, $use)){
                $base .= "<" . $tag . ">";
                array_push($use, $tag);
            }
        }
        $use = $data;
        $base .= $text; 
    }
}
print_r($base);

And here is the array if required (in JSON format!):

["This is a test
Including ",["bo","bold"],["ld",["italic","bold"]],[", ","italic"],["u",["underline","italic"]],["nderlined","underline"],", ",["strike-through","strike"],", and ",["italic","italic"],"
text:

",["numbered lists",["underline","strike","italic","bold"]],["
",[]],"as well as",["
",[]],["non ordered lists","http:\/\/test.com"],["
",[]],"it works very well",["
",[]],["try it","http:\/\/google.com"],"
",["http:\/\/google.com",["bold","http:\/\/google.com"]],"

",["wow","bold"],"
",["lol","bold"]]

Any help would be much appreciated... thanks!

  • 写回答

2条回答 默认 最新

  • drby30217 2018-02-04 21:05
    关注

    I'm honestly not sure if this is exactly what you're looking for. It would be great to have a full desired output... but I believe this is as close as it gets. It took me 3 hours so it better be it. It's a great question, very hard to accomplish.

    I did print_r(htmlentities($base)), but you can simply do print_r($base) to see the formatted result. I did that because it was easier to check with the output you provided in the question.

    Also, I modified your JSON because some tags specified there are non-existent. For example, I changed underline for u, italic for i, bold for b. Alternatives are em, strong... anyway, that's just a side-note.

    <?php
    $build = json_decode('["This is a test
    Including ",["bo","b"],["ld",["i","b"]],[", ","i"],["u",["u","i"]],["nderlined","u"],", ",["strike-through","strike"],", and ",["italic","i"],"
    text:
    
    ",["numbered lists",["u","strike","i","b"]],["
    ",[]],"as well as",["
    ",[]],["non ordered lists","http:\/\/test.com"],["
    ",[]],"it works very well",["
    ",[]],["try it","http:\/\/google.com"],"
    ",["http:\/\/google.com",["b","http:\/\/google.com"]],"
    
    ",["wow","b"],"
    ",["lol","b"]]', true);
    $used = [];
    $base = '';
    foreach($build as $data){
        if(is_array($data)){
            $text = array_shift($data);
            $tags = $data[0];
            if(!is_array($data[0])){
                $tags = [$data[0]];
            }
            $elements = '';
            $tagsToClose = array_diff($used, $tags);
            $changes = true;
            $i = 0;
            foreach($tagsToClose as $tag){
                while($changes){
                    $changes = false;
                    if($lastOpened != $tag){
                        $changes = true;
                        $elements .= '</'.$lastOpened.'>';
                        unset($used[$i++]);
                        $lastOpened = $used[$i];
                    }
                }
                $elements .= '</'.$tag.'>';
                $key = array_search($tag, $used);
                unset($used[$key]);
            }
            foreach($tags as $tag){
                if(!in_array($tag, $used)){
                    $elements .= '<'.$tag.'>';
                    array_unshift($used, $tag);
                    $lastOpened = $tag;
                }
            }
            $elements .= $text;
            $data = $elements;
        }
        $base .= $data;
    }
    unset($used);
    $base .= '</'.$lastOpened.'>';
    print_r(htmlentities($base));
    ?>
    

    EDIT

    And here's the result I got, just in case you run into some trouble testing or to check with your results or whatever:

    This is a test Including <b>bo<i>ld</i></b><i>, <u>u</u></i><u>nderlined, </u><strike>strike-through, and </strike><i>italic text: <u><strike><b>numbered lists</b></strike></u></i> as well as <http://test.com>non ordered lists</http://test.com> it works very well <http://google.com>try it <b>http://google.com </b></http://google.com><b>wow lol</b>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答