2014-09-06 105 views
0

因此,我從twitter api的查詢中獲得了json對象。在它的原始形式,文字是這樣的:從html中的T​​witter呈現Twitter emojis的最佳方式是什麼?

"check out this emoji \ud83d\udc98"

我花了很多時間閱讀Unicode和它的格式,和我已經成功具有JSON Unicode作爲密鑰庫像這樣:

$emoji_dictionary = array(
    '\ud83d\udd39'=> array(
    'emoji-id'=> 'e-B76', 
    'codepoint'=> 'U+1F539', 
    'name'=> 'SMALL BLUE DIAMOND', 
    'twitter-id'=> '1f539' 
), 
    '\ud83d\ude3f'=> array(
    'emoji-id'=> 'e-34D', 
    'codepoint'=> 'U+1F63F', 
    'name'=> 'CRYING CAT FACE', 
    'twitter-id'=> '1f63f' 
), 

    ... 
); 

所以,現在我一直在試圖像地獄評價JSON unicode的,我從Twitter得到了作爲一個字符串,然後我就可以扔在這個功能:

function get_src($str) { 
    echo 'regex found:' . $str . '<br />'; 
    return '<img class="twitter-emoji" src="https://abs.twimg.com/emoji/v1/72x72/' . $emoji_dictionary[$str]['twitter-id'] . '.png"/>'; 
    } 

哪個返回來自該表情符號的twitter的圖像,但我似乎無法正確使用PHP中的json數據preg_replace。我有時會收到此錯誤:

preg_replace(): Compilation failed: PCRE does not support \L, \l, \N{name}, \U, or \u 

我的preg_replace是這樣的(注意,這不工作):

$text = strval(json_encode($twitter_datum->text)); 
    $pattern = "/\\\\u([a-f0-9]{4})/e"; 
    $text = preg_replace($pattern, "get_src($1)", $text; 

這種模式截獲 'd83d' 和 'dc98' 分開。

我試圖做什麼不可能?我只是想從"check out this emoji!! \ud83d\udc98"

回答

3

得到1f498(從字典)爲了任何人試圖做這樣的事情,這是我學到的:東西json_encodeed

字符串操作是一個壞主意。我這樣做是因爲我看不到unicode表達式,而是小盒子而不是&因此不知道如何評估它們。

Emoji for PHP是這類事情的一個很好的資源。它可以用<span class="xxx'></span>替換任何unicode表情符號,其中xxx映射到該表情符號的精靈。它類似的東西是什麼,我試圖做的,但有兩個主要區別:

  • 代碼的正則表達式是在json_decoded實體
  • 而不是一個<img>與SRC替換它去到Twitter,這去<span>參照一個本地PNG

我的代碼現在看起來像這樣,它工作正常。唯一的問題是,如果/添加新的表情符號,它們將不會被此腳本識別。也許在這一點表情符號,會多一點普遍,W /全瀏覽器支持,等等。這是我有: $ JSON

function twitter_chron() { 
    $json = get_tweets(50); 
    $twitter_data = json_decode($json); 
    include(ABSPATH . 'wp-content/themes/custom/emoji/emoji.php'); 

    foreach($twitter_data as $twitter_datum) { 
     $id = $twitter_datum->id; 
     if (property_exists($twitter_datum, 'retweeted_status')) { 
      $text = 'RT: ' . $twitter_datum->retweeted_status->text; 
     } else { 
      $text = $twitter_datum->text; 
     } 
     $text = emoji_unified_to_html($text); 
     $text = iconv("UTF-8", "ASCII//IGNORE", $text); 
     insert_tweet($id, $text, $date); 
    } 
} 

emoji_unified_to_html($text)emoji.php。我有額外的功能,我運行的tweet身體的鏈接,標籤&提到,但我認爲它與這個特定的emojis問題無關。

希望這可以幫助別人。

相關問題