dou7466 2019-03-29 13:25 采纳率: 0%
浏览 154

使用php将印地语文本转换为Escaped Unicode字符

I want to convert hindi / Devanagari text for example "आए थे पर्यटक, खुद ही बह ग" into Unicode escaped characters like "\u0906\u090f \u0925\u0947 \u092a\u0930\u094d\u092f\u091f\u0915, \u0916\u0941\u0926 \u0939\u0940 \u092c\u0939 \u0917".

I am developing a hindi website and i have seen most of sites are using Escaped Unicode sequence inside their meta tags and schema.org. So i decided to give it a try.

i can see Hindi AKA Devanagari letters with their Escaped Unicode sequence at http://www.endmemo.com/unicode/devanagari.php

and i have also seen a tool which works the same https://www.mobilefish.com/services/unicode_escape_sequence_converter/unicode_escape_sequence_converter.php

but i cannot find any way to convert these Devanagari letters into Escaped Unicode sequence via php.

I have tried few things but nothing is working and i am not getting much help from google because all articles / forums are talking to decoding unicode escape sequence to unicode but none of them is taking about encoding..

header( 'Content-Type: text/html; charset=utf-8' ); 


function encode2($str) {
    $str = mb_convert_encoding($str , 'UTF-32', 'UTF-8');
    $t = unpack("N*", $str);
    $t = array_map(function($n) { return "&#$n;"; }, $t);
    return implode("", $t);
}

$message = "आए थे पर्यटक, खुद ही बह गए";
$message_convert = encode2($message); 
echo $message_convert;

echo "fdfdfdfdfdfdfd<br/>";


echo mb_convert_encoding($message, "HTML-ENTITIES", "auto");

I want this "आए थे पर्यटक, खुद ही बह ग" to "\u0906\u090f \u0925\u0947 \u092a\u0930\u094d\u092f\u091f\u0915, \u0916\u0941\u0926 \u0939\u0940 \u092c\u0939 \u0917"

Please help!

  • 写回答

2条回答 默认 最新

  • duanjiao5082 2019-03-29 13:34
    关注

    as suggest by @paskl i tried:

    $message = "आए थे पर्यटक, खुद ही बह गए";
    $unicode = json_encode($message)
    
    echo $unicode;
    

    And i got ""\u0906\u090f \u0925\u0947 \u092a\u0930\u094d\u092f\u091f\u0915, \u0916\u0941\u0926 \u0939\u0940 \u092c\u0939 \u0917\u090f""

    I hope it will help others who want to convert devanagari/hindi letters into Escaped Unicode sequence with php on their website.

    Thanks to @paskl

    评论

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)