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条)

报告相同问题?

悬赏问题

  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置