回显时反转 bb 代码

reverse bb-codes when echo

我在个人资料的个人消息中找到了使用 bb 代码的代码,但是当我返回设置更改消息时,它回显 html 标签而不是 bb 代码替换。

bb 代码:

        if(isset($_POST['submit'])) {

        if(isset($_POST['bio_message'])){
//BBCode Parser function
function showBBcodes($text) {
        // BBcode array
        $find = array(
                '~\[b\](.*?)\[/b\]~s',
                '~\[i\](.*?)\[/i\]~s',
                '~\[u\](.*?)\[/u\]~s',
                '~\[quote\](.*?)\[/quote\]~s',
                '~\[url\]((?:ftp|https?)://.*?)\[/url\]~s',
                '~\[img\](https?://.*?\.(?:jpg|jpeg|gif|png|bmp))\[/img\]~s'
        );
        // HTML tags to replace BBcode
        $replace = array(
                '<b></b>',
                '<i></i>',
                '<p style="text-decoration:underline;"></p>',
                '<pre></'.'pre>',
                '<a href=""></a>',
                '<img src="" alt="" />'
        );
        // Replacing the BBcodes with corresponding HTML tags
        return preg_replace($find,$replace,$text);
}
// How to use the above function:
$text = $_POST['bio_message'];
$htmltext = showBBcodes($text);

        }

            $id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');

            $bio_sql = "UPDATE users SET bio = '$htmltext' WHERE id = '$id'";
            $db->query($bio_sql);
        }else{}

在文本区域回显简介:

<?php
$id = htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
 $SQL = "SELECT * FROM users WHERE id = '$id'";


 $result = $db->query($SQL);

/* associative array */
$row = $result->fetch_array(MYSQLI_ASSOC);
print(htmlentities($row['bio'], ENT_QUOTES, 'UTF-8'));

    $result->free();
?>

试试这个功能

function showHTML($text) {
    // HTML tags to replace
    $find = array(
        '~<b>(.*?)</b>~s',
        '~<i>(.*?)</i>~s',
        '~<p style="text-decoration:underline;">(.*?)</p>~s',
        '~<pre>(.*?)</pre>~s',
        '~<a href="(.*?)">(.*?)</a>~s',
        '~<img src="(.*?)" alt="" />~s'
    );

    // BBcode array
    $replace = array(
        '[b][/b]',
        '[i][/i]',
        '[u][/u]',
        '[quote][/quote]',
        '[url][/url]',
        '[img][/img]'
    );

    // Replacing the BBcodes with corresponding HTML tags
    return preg_replace($find,$replace,$text);
}

输入:

<i>fsfsdfsf</i> <a href="http://abc.de">http://abc.de</a>

输出:

[i]fsfsdfsf[/i] [url]http://abc.de[/url]

这是我目前用来尝试回应 $row['bio']

的代码
<?php                            
     function showHTML($text) {
         // HTML tags to replace
         $find = array(
             '~<b>(.*?)</b>~s',
             '~<i>(.*?)</i>~s',
             '~<p style="text-decoration:underline;">(.*?)</p>~s',
             '~<pre>(.*?)</pre>~s',
             '~<a href="(.*?)">(.*?)</a>~s',
             '~<img src="(.*?)" alt="" />~s'
         );

         // BBcode array
         $replace = array(
             '[b][/b]',
             '[i][/i]',
             '[u][/u]',
             '[quote][/quote]',
             '[url][/url]',
             '[img][/img]'
         );

         // Replacing the BBcodes with corresponding HTML tags
         return preg_replace($find,$replace,$text);
     }

     $result = $db->query("SELECT * FROM users WHERE id='$id'");
     $row = $result->fetch_array(MYSQLI_ASSOC);

     print(showHTML($row['bio'], ENT_QUOTES, 'UTF-8'));
 ?>