Convert unicode special characters to UTF-8












0















I have problem with converting unicode characters to utf-8.
Here is my code:



<?php 
$unicode = 'u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d';

$utf8string = html_entity_decode(preg_replace("/U+([0-9A-F]{4})/", "&#x\1;", $unicode), ENT_NOQUOTES, 'UTF-8');

echo $utf8string;
?>


And it gives me below:



u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d


What did i do wrong ? any advice ?










share|improve this question























  • see here: php.net/manual/en/… and here: stackoverflow.com/questions/1805802/…

    – Jeff
    Nov 22 '18 at 2:24













  • Possible duplicate of PHP: Convert unicode codepoint to UTF-8

    – Jeff
    Nov 22 '18 at 2:26
















0















I have problem with converting unicode characters to utf-8.
Here is my code:



<?php 
$unicode = 'u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d';

$utf8string = html_entity_decode(preg_replace("/U+([0-9A-F]{4})/", "&#x\1;", $unicode), ENT_NOQUOTES, 'UTF-8');

echo $utf8string;
?>


And it gives me below:



u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d


What did i do wrong ? any advice ?










share|improve this question























  • see here: php.net/manual/en/… and here: stackoverflow.com/questions/1805802/…

    – Jeff
    Nov 22 '18 at 2:24













  • Possible duplicate of PHP: Convert unicode codepoint to UTF-8

    – Jeff
    Nov 22 '18 at 2:26














0












0








0








I have problem with converting unicode characters to utf-8.
Here is my code:



<?php 
$unicode = 'u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d';

$utf8string = html_entity_decode(preg_replace("/U+([0-9A-F]{4})/", "&#x\1;", $unicode), ENT_NOQUOTES, 'UTF-8');

echo $utf8string;
?>


And it gives me below:



u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d


What did i do wrong ? any advice ?










share|improve this question














I have problem with converting unicode characters to utf-8.
Here is my code:



<?php 
$unicode = 'u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d';

$utf8string = html_entity_decode(preg_replace("/U+([0-9A-F]{4})/", "&#x\1;", $unicode), ENT_NOQUOTES, 'UTF-8');

echo $utf8string;
?>


And it gives me below:



u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d


What did i do wrong ? any advice ?







php unicode






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 2:20









Kh Shuren-ErdeneKh Shuren-Erdene

163114




163114













  • see here: php.net/manual/en/… and here: stackoverflow.com/questions/1805802/…

    – Jeff
    Nov 22 '18 at 2:24













  • Possible duplicate of PHP: Convert unicode codepoint to UTF-8

    – Jeff
    Nov 22 '18 at 2:26



















  • see here: php.net/manual/en/… and here: stackoverflow.com/questions/1805802/…

    – Jeff
    Nov 22 '18 at 2:24













  • Possible duplicate of PHP: Convert unicode codepoint to UTF-8

    – Jeff
    Nov 22 '18 at 2:26

















see here: php.net/manual/en/… and here: stackoverflow.com/questions/1805802/…

– Jeff
Nov 22 '18 at 2:24







see here: php.net/manual/en/… and here: stackoverflow.com/questions/1805802/…

– Jeff
Nov 22 '18 at 2:24















Possible duplicate of PHP: Convert unicode codepoint to UTF-8

– Jeff
Nov 22 '18 at 2:26





Possible duplicate of PHP: Convert unicode codepoint to UTF-8

– Jeff
Nov 22 '18 at 2:26












1 Answer
1






active

oldest

votes


















1














At the very least your regular expression is looking for an uppercase U, while all your escape sequences use lower-case.



But your conversion script goes from javascript-escaped unicode characters, to HTML entities, back to a PHP string. This might be a saner solution (for this string):



$unicode = 'u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d';
echo json_decode('"' . $unicode . '"');


Be careful though, as this might break if the input string contains newlines or quotes.






share|improve this answer
























  • Nice but i have question when incoming data are dynamically many type of texts its works ?

    – Kh Shuren-Erdene
    Nov 22 '18 at 2:36











  • Check the last sentence in my answer. There are certain characters that would make this invalid JSON.

    – Evert
    Nov 22 '18 at 2:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423015%2fconvert-unicode-special-characters-to-utf-8%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














At the very least your regular expression is looking for an uppercase U, while all your escape sequences use lower-case.



But your conversion script goes from javascript-escaped unicode characters, to HTML entities, back to a PHP string. This might be a saner solution (for this string):



$unicode = 'u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d';
echo json_decode('"' . $unicode . '"');


Be careful though, as this might break if the input string contains newlines or quotes.






share|improve this answer
























  • Nice but i have question when incoming data are dynamically many type of texts its works ?

    – Kh Shuren-Erdene
    Nov 22 '18 at 2:36











  • Check the last sentence in my answer. There are certain characters that would make this invalid JSON.

    – Evert
    Nov 22 '18 at 2:50
















1














At the very least your regular expression is looking for an uppercase U, while all your escape sequences use lower-case.



But your conversion script goes from javascript-escaped unicode characters, to HTML entities, back to a PHP string. This might be a saner solution (for this string):



$unicode = 'u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d';
echo json_decode('"' . $unicode . '"');


Be careful though, as this might break if the input string contains newlines or quotes.






share|improve this answer
























  • Nice but i have question when incoming data are dynamically many type of texts its works ?

    – Kh Shuren-Erdene
    Nov 22 '18 at 2:36











  • Check the last sentence in my answer. There are certain characters that would make this invalid JSON.

    – Evert
    Nov 22 '18 at 2:50














1












1








1







At the very least your regular expression is looking for an uppercase U, while all your escape sequences use lower-case.



But your conversion script goes from javascript-escaped unicode characters, to HTML entities, back to a PHP string. This might be a saner solution (for this string):



$unicode = 'u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d';
echo json_decode('"' . $unicode . '"');


Be careful though, as this might break if the input string contains newlines or quotes.






share|improve this answer













At the very least your regular expression is looking for an uppercase U, while all your escape sequences use lower-case.



But your conversion script goes from javascript-escaped unicode characters, to HTML entities, back to a PHP string. This might be a saner solution (for this string):



$unicode = 'u0411. u0426u044du0446u044du0433u0441u04afu0440u044du043d';
echo json_decode('"' . $unicode . '"');


Be careful though, as this might break if the input string contains newlines or quotes.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 2:32









EvertEvert

40.6k1569123




40.6k1569123













  • Nice but i have question when incoming data are dynamically many type of texts its works ?

    – Kh Shuren-Erdene
    Nov 22 '18 at 2:36











  • Check the last sentence in my answer. There are certain characters that would make this invalid JSON.

    – Evert
    Nov 22 '18 at 2:50



















  • Nice but i have question when incoming data are dynamically many type of texts its works ?

    – Kh Shuren-Erdene
    Nov 22 '18 at 2:36











  • Check the last sentence in my answer. There are certain characters that would make this invalid JSON.

    – Evert
    Nov 22 '18 at 2:50

















Nice but i have question when incoming data are dynamically many type of texts its works ?

– Kh Shuren-Erdene
Nov 22 '18 at 2:36





Nice but i have question when incoming data are dynamically many type of texts its works ?

– Kh Shuren-Erdene
Nov 22 '18 at 2:36













Check the last sentence in my answer. There are certain characters that would make this invalid JSON.

– Evert
Nov 22 '18 at 2:50





Check the last sentence in my answer. There are certain characters that would make this invalid JSON.

– Evert
Nov 22 '18 at 2:50


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423015%2fconvert-unicode-special-characters-to-utf-8%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen