2016-08-13 6 views
0

プロファイル上の個人的なメッセージにbbコードを使用するコードが見つかりましたが、メッセージを変更する設定に戻ると、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>$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{} 

は、テキストエリアにバイオをエコー

<?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(); 
?> 
+0

さて、あなたはまったく同じだけの他の方法でラウンドを行うエコーしようとする使用していたコードです。 –

+0

は試しましたが動作しません。 $ findと$ replaceが並び替えられ、順序が入れ替えられました。 –

+0

正規表現と置換を編集する必要があります。 –

答えて

0

この機能を試してみてください

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); 
} 

入力:

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

出力:

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

まずPHPコードに関数を追加し、showHTML($ text)で呼び出すと –

+0

は動作しません。 私はそれをエコーするためにshowBBcodes($ text)が必要ですが、それが追加されたときにその部分の下のすべてをカットするので、エラーや何かを得ます。 –

+0

PHPの基本、特に関数とその使い方を読むことをお勧めします。元の関数を実行して、別のファイル、例えばfuntions.phpに移動し、include_once( 'functions.php');でそれを組み込む必要があります。次に、HTMLをBBCodeに置き換えたいときにshowHTMLメソッドを呼び出し、HTMLをBBcodeに変換するときにshowBBcodeを呼び出すだけです.BBcode –

0

これは私が現在$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]$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); 
    } 

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

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

ああ、それは私の悪いことがわかります。 'showHTML($行[ 'バイオ']、ENT_QUOTES、 'UTF-8')を交換してください' 'にhtmlentities(showHTML($行[ 'バイオ'])、ENT_QUOTES、 'UTF-8')' –

+0

いや、まだ何も。それは私のプロファイルに提出する方法を保存し、エコー後にテキストエリアではない –

+0

私にとっては、あなたは単にSQLクエリを実行し、データベースからデータを取得しないように見えます。 $ row ['bio']はあなたのフェッチの結果です。 –