2016-08-13 22 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와 $ 교체가 다시 시작되어 순서가 바뀌 었습니다. –

+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> 
,617,

출력은 :

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

먼저 PHP 코드에 함수를 추가 한 다음 showHTML ($ text) –

+0

으로 호출하면 작동하지 않습니다. showBBcodes ($ text)를 사용하면 에코를 얻을 수 있지만 추가 할 때 그 부분 아래의 모든 내용이 잘려서 오류가 발생합니다. –

+0

PHP 기본, 특히 기능 및 용도를 읽는 것이 좋습니다. 원래 함수와 광산을 별도의 파일 (예 : funtions.php)로 옮겨서 include_once ('functions.php');를 통해 포함시켜야합니다. 그럼 당신이해야 할 일은 showHTML 메소드를 호출하여 BBCode를 HTML로 바꾸고 showBBcode를 HTML을 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

아하 나의 그것이 나빴던 것을 안다. ENT_QUOTES, 'UTF-8')' –

+0

', htmlentities (showHTML ($ row ['bio '])로'showHTML ($ row ['bio '], ENT_QUOTES,'UTF-8 ' . 그것은 저장하고 내 프로필에 그것을 원하지만 제출 후 textarea에 원하는대로 반향 –

+0

나를 위해 그것은 당신이 단지 SQL 쿼리를 실행하고 심지어 데이터베이스에서 데이터를 가져 오지 않는 것 같습니다. $ row [ 'bio']는 가져온 결과입니다. –