使用php查找和解析自定义标签

时间:2018-09-28 20:19:01

标签: php html

我想为网站用户创建一个可变模板。例如,我想搜索一个HTML代码,找到一个自定义标签并用PHP对其进行解析。实际上,我想从数据库中获取以下代码,并同时在HTML和AMPHTML中使用它。

HTML代码:

<div>
   <p>some text here . . .</p>

   [image]http://foo.com/image-number-1.jpg[/image]

   <p>some text here . . .</p>

   [image]http://foo.com/image-number-2.jpg[/image]

   <p>some text here . . .</p>

   [image]http://foo.com/image-number-3.jpg[/image]

</div>

如何在上述代码中找到[image][/image]标签并提取图像网址separately并将其放入<img><amp-img>标签中,如下所示:

<div>
   <p>some text here . . .</p>

   <img src="http://foo.com/image-number-1.jpg" + some attributes>

   <p>some text here . . .</p>

   <img src="http://foo.com/image-number-2.jpg" + some attributes>

   <p>some text here . . .</p>

   <img src="http://foo.com/image-number-3.jpg" + some attributes>

</div>

我知道我可以将[image]替换为<img src=",依此类推,但是后来我无法动态更改单个图像的属性。

1 个答案:

答案 0 :(得分:0)

很多年前,我编写了此函数,并在许多站点上使用了它。大概有一百万种变化。确保清理用户输入内容,否则将遇到XSS问题。

<?php
function displayBBCode($content, $allowHtml=true)
{
    if(!$allowHtml)
    {
        //Make safe any html
        $content = htmlspecialchars($content);
    }

    //Make sure there is no whitespace at the end of the message
    //It's conceivable that the user will start their message with whitespace
    $content = chop($content);

    //Arrays for the bbCode replacements
    $bbcodeRegex = array(
        0 => '/\[b\](.+?)\[\/b\]/s',
        1 => '/\[i\](.+?)\[\/i\]/s',
        2 => '/\[u\](.+?)\[\/u\]/s',
        3 => '/\[quote\](.+?)\[\/quote\]/s',
        4 => '/\[quote\=(.+?)](.+?)\[\/quote\]/s',
        5 => '/\[url\](.+?)\[\/url\]/s',
        6 => '/\[url\=(.+?)\](.+?)\[\/url\]/s',
        7 => '/\[color\=(.+?)\](.+?)\[\/color\]/s',
        8 => '/\[size\=(.+?)\](.+?)\[\/size\]/s',
        9 => '/\[img\](.+?)\[\/img\]/s',
        10 => '/\[image\](.+?)\[\/image\]/s'
    );

    $bbcodeReplace = array(
        0 => '<b>$1</b>',
        1 => '<i>$1</i>',
        2 => '<u>$1</u>',
        3 => '<table class="quote"><tr><td>Quote:</td></tr><tr><td class="quote_box">$1</td></tr></table>',
        4 => '<table class="quote"><tr><td>$1 said:</td></tr><tr><td class="quote_box">$2</td></tr></table>',
        5 => '<a href="$1" target="_blank">$1</a>',
        6 => '<a href="$1" target="_blank">$2</a>',
        7 => '<span style="color:$1">$2</span>',
        8 => '<span style="font-size:$1">$2</span>',
        9 => '<img src="$1">',
        10 => '<img src="$1">'
    );

    ksort($bbcodeRegex);
    ksort($bbcodeReplace);

    //preg_replace to convert all remaining bbCode tags
    $contentBbcodeTreated = preg_replace($bbcodeRegex, $bbcodeReplace, $content);

    //Convert new lines to <br />
    $contentWithBr = nl2br($contentBbcodeTreated);

    return $contentWithBr;
}

$input = <<<END
<div>
   <p>some text here . . .</p>
   [image]http://foo.com/image-number-1.jpg[/image]
   <p>some text here . . .</p>
   [image]http://foo.com/image-number-2.jpg[/image]
   <p>some text here . . .</p>
   [image]http://foo.com/image-number-3.jpg[/image]
</div>
END;

$output = displayBBCode($input);

echo $output;