简体   繁体   English

PHP-回显数组中标签的开始和结束

[英]PHP - echo the opening and closing of tags from array

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: 但是,数组中的其他内容是引用它们所对应的HTML标记的,例如:

    [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... 这是到目前为止我拥有的PHP代码...

$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!): 这是数组(如果需要)(JSON格式!):

["This is a test\nIncluding ",["bo","bold"],["ld",["italic","bold"]],[", ","italic"],["u",["underline","italic"]],["nderlined","underline"],", ",["strike-through","strike"],", and ",["italic","italic"],"\ntext:\n\n",["numbered lists",["underline","strike","italic","bold"]],["\n",[]],"as well as",["\n",[]],["non ordered lists","http:\/\/test.com"],["\n",[]],"it works very well",["\n",[]],["try it","http:\/\/google.com"],"\n",["http:\/\/google.com",["bold","http:\/\/google.com"]],"\n\n",["wow","bold"],"\n",["lol","bold"]]

Any help would be much appreciated... thanks! 任何帮助将不胜感激...谢谢!

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. 我花了3个小时才做的更好。 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. 我做了print_r(htmlentities($base)) ,但是您可以简单地执行print_r($base)来查看格式化结果。 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. 另外,我修改了您的JSON,因为其中指定的某些标签不存在。 For example, I changed underline for u , italic for i , bold for b . 例如,我将underline更改为u ,将italic更改为i ,将bold更改为b Alternatives are em , strong ... anyway, that's just a side-note. 替代方案是emstrong ……无论如何,这只是一个旁注。

<?php
$build = json_decode('["This is a test\nIncluding ",["bo","b"],["ld",["i","b"]],[", ","i"],["u",["u","i"]],["nderlined","u"],", ",["strike-through","strike"],", and ",["italic","i"],"\ntext:\n\n",["numbered lists",["u","strike","i","b"]],["\n",[]],"as well as",["\n",[]],["non ordered lists","http:\/\/test.com"],["\n",[]],"it works very well",["\n",[]],["try it","http:\/\/google.com"],"\n",["http:\/\/google.com",["b","http:\/\/google.com"]],"\n\n",["wow","b"],"\n",["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>

After many hours, this was my solution that I ended up with: 几个小时后,这是我最终得到的解决方案:

$build = json_decode('["This is a test Including\u00a0",["bo","bold"],["ld",["italic","bold"]],[",\u00a0","italic"],["u",["underline","italic"]],["nderlined,\u00a0","underline"],"strike-through, and\u00a0",["italic text:\u00a0","italic"],"it works very well\u00a0try it\u00a0",["http:\/\/google.com",["bold","http:\/\/google.com"]],["\u00a0wow lol","bold"]]',true);
$standard = array("bold"=>"b","underline"=>"u","strike"=>"s","italic"=>"i","link"=>"a","size"=>null);
$lists = array("ordered"=>"ol","bullet"=>"ul");
$size = array("huge"=>"2.5em","large"=>"1.5em");

$base = "";
foreach($build as $part){
    $use = array();
    $tags = true;
    $len = 1;
    if(!is_array($part) or count($part) == 1){
        $text = $part;
        $tags = false;
        $part = array();
    } else {
        $text = array_shift($part);

        if(count($part) == 1){
            if(is_array($part[0])){
                $part = $part[0];
            }
        }
        if(!is_array($part)){
            $part = array($part);
        }
    }
    if($tags){
        foreach ($part as $tag) {
            if(!in_array($tag, array_keys($standard)) && !in_array($tag, array_keys($lists)) && !in_array($tag, array_keys($size))){
                $base .= '<a href="' . $tag . '" title="' . $tag . '" class="link">';
                $tag = "link";
            } elseif(in_array($tag, array_keys($size))){
                $base .= "<span style='font-size:" . $size[$tag] . "'>";
            } elseif(!in_array($tag, array_keys($lists))) {
                $base .= "<" . $standard[$tag] . ">";
            }
            array_push($use, $tag);
        }
        $base .= $text;
        foreach (array_reverse($part) as $tag) {
            if(!in_array($tag, array_keys($standard)) && !in_array($tag, array_keys($lists)) && !in_array($tag, array_keys($size))){
                $base .= '</a>';
            } elseif(in_array($tag, array_keys($size))){
                $base .= "</span>";
            } elseif (!in_array($tag, array_keys($lists))) {
                $base .= "</" . $standard[$tag] . ">";
            }
            array_push($use, $tag);
        }
    } else {
        $base .= $text;
    }
}
print_r($base);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM