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

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向