用div标签替换方括号

时间:2016-03-24 22:19:39

标签: php

有人可以帮我解决这个问题: 例如,我有这个字符串:

"
[row]
[col-md-6 col-lg-3]
Lorem ipsum sit dolor amet... And the rest of the text
[/col]
[/row]
"

用PHP替换这个方括号是否有任何方法 <div class="<some_dynamic_tags>"> 这是我的一些代码。它仅适用于关闭标签([/ col],[/ row])。仍然不知道如何解决这个问题来打开标签。

class Html extends BaseHtml
{
    public static function containsChar($haystack, $needle)
    {
        $pos = strpos($haystack, $needle);
        if ($pos === false)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    public static function doShortcode($string)
    {
        if (Html::containsChar($string, '/'))
        {
            // This is a closing tag
            return '</div>';
        }
        else
        {
            // This is an opening tag

        }
    }

    public static function bootstrapShortcode($text)
    {
        preg_match_all("/\[([^\]]*)\]/", $text, $matches);

        $htmlText = $matches[1];
        echo nl2br($text) . '<br>';
        foreach ($htmlText as $val) {
            var_dump(Html::doShortcode($val));
        }
        return $htmlText;
    }
}

2 个答案:

答案 0 :(得分:0)

有两种方法可以做到这一点

<?php
 echo "<div class='yourClass'>'{$yourVariable}'</div>"; 
?>

or

<?php
 echo '<div class="yourClass">"{$yourVariable}"</div>';
?>

取决于您的需求。

答案 1 :(得分:0)

您可能正在寻找符合

的内容
    return '<div class="'.substr($string, 1, strlen($string) - 2).'">';

$string"[col-md-6]"时,会返回<div class="col-md-6">

相关问题