2016-07-16 1 views
0

이 웃음 컨벤터 http://www.bitrepository.com/how-to-convert-smilies-to-graphics.html을 사용하여 웃음을 Laravel로 그래픽으로 변환하는 함수를 만들고 싶습니다. 그래서, 제 질문은이 코드를 어디에 넣어야합니까? 비공개 메시지 또는 다른 곳에 동일한 컨버터를 사용해야 할 수도 있으므로 나중에 채팅 컨트롤러에서 모든 변환 작업을 수행하고 싶지 않습니다. 나중에 다시 덮어 쓸 필요가 있기 때문입니다.laravel로 미소 변환기를 만들 수있는 곳

답변

3

이것을 서비스 클래스로 가져옵니다.

namespace App\Services; 

class SmileService 
{ 
    function convertSmilies($text, $url_address_to_images_folder) // with '/' at the end 
    { 
     $array = array(':-)' => 'smile001.gif', // happy 
     ':)' => 'smile001.gif', // happy 
     ':-D' => 'smile002.gif', // very happy 
     ':D' => 'smile002.gif', // very happy 
     ':-O' => 'smile008.gif', // surprised/o, no 
     ':-P' => 'smile007.gif', // tongue sticking out 
     ':P' => 'smile007.gif', // tongue sticking out 
     ';-)' => 'smile009.gif', // wink 
     ';)' => 'smile009.gif', // wink 
     ':-(' => 'smile003.gif', // sad 
     ':(' => 'smile003.gif', // sad 
     '8o|' => 'smile011.gif', // angry grin 
     ':[email protected]' => 'smile010.gif', // angry 
     '8-)' => 'smile027.gif', // nerd 
     ":'(" => "smile040.gif", // crying 
     ':-S' => 'smile005.gif', // confused 
     ':-$' => 'smile004.gif', // embarrassed 
     ':-|' => 'smile012.gif', // undecided 
     ':-*' => 'smile055.gif', // kissing 
     ':-#' => 'smile056.gif', // don't tell anyone 
     '(H)' => 'smile006.gif', // wacked out sunny face 
     '<:o)' => 'smile041.gif', // party 
     '(A)' => 'angel.gif', // angel 
     '+o(' => 'Bo.gif', // Sick 
     '(brb)' => 'smile043.gif', // be right back 
     '(6)' => 'smile039.gif', // devil 
     '(Y)' => 'smile049.gif', // yes 
     '(N)' => 'smile050.gif', // no 
     '(X)' => 'grl.gif', // girl 
     '(Z)' => 'boy.gif', // boy 
     '(L)' => 'smile015.gif', // love 
     '(U)' => 'smile016.gif', // don't love 
     '(K)' => 'smile020.gif', // kiss 
     '(P)' => 'pic.gif', // picture 
     '(G)' => 'gift.gif', // gift 
     '(%)' => 'smile037.gif', // handcuffs 
     '(F)' => 'smile019.gif', // flower 
     '(W)' => 'smile018.gif', // Wilt flower 
     '(D)' => 'smile036.gif', // drink 
     '(B)' => 'smile035.gif', // beer 
     '(C)' => 'coffee.gif', // cup 
     '(^)' => 'smile054.gif', // (Birthday) cake 
     '(pi)' => 'pi.gif', // pizza 
     '(||)' => 'smile047.gif', // chopsticks 
     '(M)' => 'm.gif', // messenger 
     '(@)' => 'cat.gif', // cat 
     '(sn)' => 'sn.gif', // snail 
     '(bah)' => 'bah.gif', // sheep 
     '(tu)' => 'smile042.gif', // turtel 
     '(&)' => 'dog.gif', // dog 
     ':-[' => 'smile034.gif', // Bat 
     '(?)' => 'smile038.gif', // ASL - Age Sex Location 
     '({)' => 'smile026.gif', // hug left 
     '(})' => 'smile025.gif', // hug right 
     '(pl)' => 'smile048.gif', // plate 
     '(I)' => 'light.gif', // idea 
     '(8)' => 'music.gif', // music 
     '(ip)' => 'ip.gif', // island 
     '(S)' => 'smile021.gif', // asleep/moon 
     '(*)' => 'smile022.gif', // star 
     '(R)' => 'smile024.gif', // rainbow 
     '(#)' => 'smile023.gif', // sun 
     '(li)' => 'smile052.gif', // lightning 
     '(st)' => 'smile051.gif', // storm/rain 
     '(um)' => 'um.gif', // umbrella 
     '(co)' => 'co.gif', // computer 
     '(mp)' => 'mp.gif', // mobile phone 
     '(T)' => 'phone.gif', // telephone 
     '(E)' => 'mail.gif', // email 
     '(ap)' => 'ap.gif', // airplane 
     '(au)' => 'au.gif', // car 
     '(~)' => 'movie.gif', // movie 
     '(O)' => 'time.gif', // time/clock 
     '(so)' => 'so.gif', // soccer ball 
     '(ci)' => 'ci.gif', // cigarette 
     '(yn)' => 'smile046.gif', // fingers crossed 
     '(h5)' => 'smile045.gif', // high five 
     '(xx)' => 'smile044.gif', // x-box 
     '(mo)' => 'mo.gif'); // money 

     foreach($array as $s => $xc) 
     { 
      $text = str_replace($s, "<img align='absmiddle' src='".$url_address_to_images_folder.$xc."'>", $text); 
     } 

     return $text; 
    } 
} 
당신의 ChatController에서 지금

(또는 다른 컨트롤러)

use App\Services\SmileService; 

class ChatController 
{ 

    protected $smileService; 

    public function __construct() 
    { 
     $this->smileService = new SmileService(); 
    } 

    public function saveMessage(Request $request) 
    { 
     // some code here 
     $chatModel = new Chat(); 

     // Now convert your smilies 
     $convertedMessage = $this->SmileService->convertSmilies($request->message); 

     $chatModel->message = $convertedMessage; 

     $chatModel->save(); 
    } 

    // Could also do this when you retrieve messages 
    // from the controller instead, so that you always 
    // have the same text in the DB. That way if you 
    // ever change your smile images, your data would still be intact. 
} 

말했다 모든이는 Angular filter을위한 좋은 기회입니다.

+0

감사! 하지만 도우미 기능을 만들었고 나는 그것이 잘 작동한다고 생각합니다! – Danielius