2013-04-05 100 views
0

其实,我已经搜索了很多,我也探讨过这个论坛,但这是我的第二天,我找不到解决方案。如何将Html代码转换为相关的Unicode字符

我的问题是,我想在HTML代码

باخ 

转换为其equallent Unicode字符

خ ا ب 

其实我不希望所有的HTML符号转换为Unicode字符。我只想将阿拉伯语/乌尔都语html代码转换为unicode字符。这些字符的范围是from ؛ To ۹如果没有任何PHP函数,那么我怎样才能用它们的一致的unicode字符替换代码?

+0

是否有一个原因,你只会将一些HTML字符引用转换为字符?它会更容易,它会节省字节,将它们全部转换。 – 2013-04-05 10:40:22

+0

@ JukkaK.Korpela如果我将它们全部转换,然后'<'和'>'也将转换为'<' and '>',这是我不想要的。 – Munib 2013-04-05 10:55:10

回答

0

你是否尝试在html头中使用utf-8编码?

<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
0

试试这个

<?php 
$trans_tbl = get_html_translation_table(HTML_ENTITIES); 
foreach($trans_tbl as $k => $v) 
{ 
    $ttr[$v] = utf8_encode($k); 
} 
$text = '&#1576;&#1576;....;&#1582'; 
$text = strtr($text, $ttr); 
echo $text; 
?> 

为MySQL解决方案,你可以设置的字符集为

$mysqli = new mysqli($host, $user, $pass, $db); 

    if (!$mysqli->set_charset("utf8")) { 
    die("error"); 

    } 
4

我认为你正在寻找:

html_entity_decode('&#1576;&#1575;&#1582;', ENT_QUOTES, 'UTF-8'); 

当你从&#1576;到ب,这就是所谓的解码。做相反的事情叫做编码。

至于只替换&#1563;到&#1785;也许尝试这样的事情。

<?php 

// Random set of entities, two are outside the 1563 - 1785 range. 
$entities = '&#1563;&#1564;&#60;&#1604;&#241;&#1784;&#1785;'; 

// Matches entities from 1500 to 1799, not perfect, I know. 
preg_match_all('/&#1[5-7][0-9]{2};/', $entities, $matches); 

$entityRegex = array(); // Will hold the entity code regular expression. 
$decodedCharacters = array(); // Will hold the decoded characters. 

foreach ($matches[0] as $entity) 
{ 
    // Convert the entity to human-readable character. 
    $unicodeCharacter = html_entity_decode($entity, ENT_QUOTES, 'UTF-8'); 

    array_push($entityRegex, "/$entity/"); 
    array_push($decodedCharacters, $unicodeCharacter); 
} 

// Replace all of the matched entities with the human-readable character. 
$replaced = preg_replace($entityRegex, $decodedCharacters, $entities); 

?> 

尽我所能解决这个问题。希望这会有所帮助。现在是我现在的5点,所以我要睡觉了! :)

+0

但问题是在我不想解码的同一个html字符串中还有其他html字符。我怎样才能跳过它们? – Munib 2013-04-05 10:38:37

+0

哦。这很棘手。首先想到的是使用正则表达式......其他人是否有更好的主意? – 425nesp 2013-04-05 11:00:56

相关问题