2017-05-11 9 views
1

チャットで太字のを1つのアスタリスクにする正規表現の呼び出しを行いたいと思いました。アスタリスク間のテキストを太字にします

次のようなものです:テスト! * こんにちは、これはPHPスクリプトです *

誰が私を助けることができますか?私はチャットルームのためのPHPスクリプトのプロセスのいくつかを投稿しましたが、正規表現は表示されません。

(*)+([^。*?] +)+(*)は私がこれを想定している正規表現です。チャットルームに「送信」してチャットルームに追加して表示する部分を含めました。

私はここで間違っていますか?私はこれを理解しようと何時間も座っている。ありがとう。

case('send'): 
     $message = htmlentities(strip_tags($_POST['message'])); 
     $nickname = htmlentities(strip_tags($_POST['username'])); 

      $text = "/(\*)+([^.*?$]+)+(\*)"; 
     $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; 
     $avc = htmlentities(strip_tags($_POST['avatarC'])); 
     $rbc = htmlentities(strip_tags($_POST['robeC'])); 




    $message = str_replace("\n", "", $message); 

     if(($message) != "") 
     { 
      $temp_msg = ""; 
      if(strlen($message)>75) 
      { 
       $temp_msg = wordwrap($message, 75, "<br />"); 

      } 
      else 
      { 
      $temp_msg = $message; 


      } 




     /*if(preg_match($reg_exUrl, $avc, $url)) { 
      $avc = preg_replace($reg_exUrl, '<a href="'.$url[0].'" target="_blank">'.$url[0].'</a>', $avc); 
     } 
     $avc = str_replace("\n", " ", $avc);*/ 


     $current_date = date('H:i:s'); 

     //fwrite(fopen($file, 'a'), "<div class=\"message_container message_cell\"><img class=\"img_message\" src=$avc></img><img class=\"img_message\" src=$rbc></img><span class=\"message_text\"><span class=\"message_user\">"<b>. $nickname ."</b> ".$current_date."</span>". $message ."</span></div><br />\n"); 
     fwrite(fopen($file, 'a'), "<div class=\"message_container message_cell\"><img class=\"img_message\" src=$avc></img><img class=\"img_message\" src=$rbc></img><span class=\"message_text\"><span class=\"message_user\"><b>" . $nickname ."</b> [".$current_date."]:</span><br /><br/>". $temp_msg ."</span></div><br />\n"); 
     } 
     break; 

    case('addToChat'): 
    $lines = file($file); 
    $fristCount = count($lines); 
    $nickname = $_POST['username']; 
    fwrite(fopen($file, 'a'), "<div class=\"message_cell message_container\">".$nickname."</div><div>\n"); 

    $text = array(); 
    $lines = file($file); 



    $log['file'] = $file; 


    foreach ($lines as $line_num => $line) { 
    $line = str_replace("\n", "", $line); 
    if($line != ""){ 
     $text[] = $line; 
    } 
    } 

答えて

0

チャットであ​​なたのメッセージは、その種の単一のパターンを持っているとしている場合、スクリプトの下に動作します:

<?php 
$message="Test! * Hello This is a PHP script * and this another one"; 
$pattern = "/(?<=\*).+?(?=\*)/"; 
preg_match($pattern, $message,$matches);  
$temp_msg=$message; 
for($i=0;$i<count($matches);$i++){  
    $temp_msg=str_replace($matches[$i],"<b>".$matches[$i]."</b>",$message);  
} 
$temp_msg=str_replace("*","",$temp_msg); 
echo $temp_msg; 
?> 
関連する問題