doure8758 2016-08-13 13:49
浏览 35
已采纳

当回声时反向bb码

I have found a code to use bb-codes in my personal message on profiles, but when i go back to settings to change the message it echo html tags instead of the bb-codes replacement.

bb-codes :

        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>$1</b>',
                '<i>$1</i>',
                '<p style="text-decoration:underline;">$1</p>',
                '<pre>$1</'.'pre>',
                '<a href="$1">$1</a>',
                '<img src="$1" 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{}

echo bio in textarea:

<?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();
?>
  • 写回答

2条回答 默认 最新

  • donglei1616 2016-08-13 14:19
    关注

    Try this function

    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]$1[/b]',
            '[i]$1[/i]',
            '[u]$1[/u]',
            '[quote]$1[/quote]',
            '[url]$1[/url]',
            '[img]$1[/img]'
        );
    
        // Replacing the BBcodes with corresponding HTML tags
        return preg_replace($find,$replace,$text);
    }
    

    Input:

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

    Output:

    [i]fsfsdfsf[/i] [url]http://abc.de[/url]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来