PHP UTF8 unicode bu tür dönüştürme

5 Cevap php

PHP okunabilir UTF8 metne bu dönüştürmek için çalışıyorum

Tel Aviv-Yafo (Hebrew: \u05ea\u05b5\u05bc\u05dc\u05be\u05d0\u05b8\u05d1\u05b4\u05d9\u05d1-\u05d9\u05b8\u05e4\u05d5\u05b9; Arabic: \u062a\u0644 \u0623\u0628\u064a\u0628\u200e, Tall \u02bcAb\u012bb), usually called Tel Aviv

Bunu nasıl herhangi bir fikir?

Online birkaç yöntem denedim, ama bir tane bulamadım.

Bu durumda ben, İbranice ve Arapça unicode var

5 Cevap

this comment sayısal koddan bir unicode karakter almak için bir yol için bkz. Sonra, bir regex o eşdeğer karakteri ile her \uXXXX desen yerini alacak yerini yazabilirsiniz.

Alternatif olarak, onun eşleştirme &#XXXX; html varlık formu ile her \uXXXX desen yerini alabilir, ve daha sonra aşağıdaki kullanın:

mb_convert_encoding(string_with_html_entities, 'UTF-8', 'HTML-ENTITIES');

Daha tam bir örnek:

// The four \\\\ in the pattern here are necessary to match \u in the original string
$replacedString = preg_replace("/\\\\u(\d{4})/", "&#$1;", $originalString);
$unicodeString = mb_convert_encoding($replacedString, 'UTF-8', 'HTML-ENTITIES');

Sen onaltılık sayılar kullanılır belirtmek için yedek dizesindeki '#' sonra 'x' eklemek gerekir.

$replacedString = preg_replace("/\\\\u(\d{4})/", "&#x$1;", $originalString);
$unicodeString = mb_convert_encoding($replacedString, 'UTF-8', 'HTML-ENTITIES');

None of the other answers work perfectly as is. I've combined them together and my addition results in this one:

$replacedString = preg_replace("/\\\\u([0-9abcdef]{4})/", "&#x$1;", $originalString);
$unicodeString = mb_convert_encoding($replacedString, 'UTF-8', 'HTML-ENTITIES');

Bu kesinlikle iş yapar :)

Bu kod çalışıyorum:

function unicode_conv($originalString) {
  // The four \\\\ in the pattern here are necessary to match \u in the original string
  $replacedString = preg_replace("/\\\\u(\d{4})/", "&#$1;", $originalString);
  $unicodeString = mb_convert_encoding($replacedString, 'UTF-8', 'HTML-ENTITIES');
  return $unicodeString;
}

echo unicode_conv("Tel Aviv-Yafo (Hebrew: \u05ea\u05b5\u05bc\u05dc\u05be\u05d0\u05b8\u05d1\u05b4\u05d9\u05d1-\u05d9\u05b8\u05e4\u05d5\u05b9; Arabic: \u062a\u0644 \u0623\u0628\u064a\u0628\u200e, Tall \u02bcAb\u012bb), usually called Tel Aviv, is the second largest city in Israel, with an estimated population of 393,900. The city is situated on the Israeli Mediterranean coast, with a land area of 51.8\u00a0square kilometres (20.0\u00a0sq\u00a0mi). It is the largest and most populous city in the metropolitan area of Gush Dan, home to 3.15\u00a0million people as of 2008. The city is governed by the Tel Aviv-Yafo municipality, headed by Ron Huldai.\nTel Aviv was founded in 1909 on the outskirts of the ancient port city of Jaffa (Hebrew: \u05d9\u05b8\u05e4\u05d5\u05b9\u200e, Yafo; Arabic: \u064a\u0627\u0641\u0627\u200e, Yaffa). The growth of Tel Aviv soon outpaced Jaffa, which was largely Arab at the time. Tel Aviv and Jaffa were merged into a single municipality in 1950, two years after the establishment of the State of Israel. Tel Aviv's White City, designated a UNESCO World Heritage Site in 2003, comprises the world's largest concentration of Modernist-style buildings.\nTel Aviv is classified as a beta+...");

Sonuç doğru değil, gerçekten bir fark çok yapmaz, bir kaç harf Rus / Yunanca ve İbranice değil / Arapça değiştirilir.

Onun varlık numarası gibi yanlıştır.

I encountered the same problem recently, so was glad to see this question. Doing some tests, I found the following code works:

$replacedString = preg_replace("/\\\\u([0-9abcdef]{4})/", "&#x$1;", $original_string);
//$unicodeString    = mb_convert_encoding($replacedString, 'UTF-8', 'HTML-ENTITIES'); 

The only thing I changed is that I commented out the 2nd line of code. Webpage, however, must be set to display UTF-8.

Tadını çıkarın!