0

(Sorry if my english is bad, but is not my language)

In my PHP file I receive an object called $ orderdetalle that is generated in a part of the server where I do not have access and I don't know the encode of the server ...$ orderdetalle is an array that contains, in string fields, different values ​​and between they the lastname. But if the lastname is for example, Hernández the variable keepsHern\u00e1ndez.

So that the lastname can later send it in a json, I am trying to do what I read in another similar question with str_replace() and preg_replace():

$lastname_consumer = $ordendetalle['customer_lastname'];//I receive the lastname Hern\u00e1ndez
$str_lastname = str_replace('\u','u',$lastname_consumer);
$lastname = preg_replace('/u([\da-fA-F]{4})/', '&#x\1;', $str_lastname);

$customer = array ( 'customer' => array ( 'id' => $ordendetalle['customerId'], 
                                          'lastname' => $lastname, 
                                          'firstname' => $ordendetalle['firstname'],
                                          'email' => $ordendetalle['email']
                                        )
                  );

$customer_order = print_r(json_encode($customer), true); //Print in a log

My problem is that $str_lastname gets Hern\u00e1ndez instead of Hernu00e1ndez when doing the str_replace(), and its worthless to subsequently do the preg_replace() and gets Hernández.

On the other hand, I have tried the code in http://phptester.net/ and it works perfectly for me but I understand that it is a matter of \u00e1 being a character UTF-8. So I do not know very well how to advance at this point.

Thanks for your time!

EDIT: Add var_dump($ordendetalle):

["customer_id"]=> string(3) "979"
["customer_email"]=> string(23) "email@test.es"
["customer_firstname"]=> string(7) "UserTest"
["customer_lastname"]=> string(10) "Hernández"
Exe
  • 101
  • 3
  • https://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha#comment23799170_7981441 suggests using (abusing?) `json_decode` for this. – Tobias K. Aug 07 '18 at 07:25
  • 1
    Are you sure you're not simply dealing with JSON here…? – deceze Aug 07 '18 at 07:45
  • @TobiasK `"lastname": "Hern\u00e1ndez"` print in the log when `print_r(json_decode('{"lastname": "'.$lastname_consumer.'"}'));` – Exe Aug 07 '18 at 07:47
  • 1
    Please give us a clear sample of your data with `var_dump($ordendetalle)`. – deceze Aug 07 '18 at 07:49
  • The `json_decode` approach works for me: http://sandbox.onlinephpfunctions.com/code/b839dd137d3261f254c3352bdf86479ebede2efc – Tobias K. Aug 07 '18 at 08:00
  • In http://phptester.net/ also works... but in real... – Exe Aug 07 '18 at 08:01
  • @deceze var_dump added – Exe Aug 07 '18 at 08:43
  • There's no `\u...` in that `var_dump`…?! Guessing from the `string(10)` the string is nicely encoded in UTF-8…!? – deceze Aug 07 '18 at 08:58
  • 1
    I'm guessing you might want to read [Reference: Why are my “special” Unicode characters encoded weird using json_encode?](https://stackoverflow.com/q/22745662/476) – deceze Aug 07 '18 at 09:00
  • Ohh works! If you write an answer I accept it right now :) Thanks @deceze – Exe Aug 07 '18 at 09:37

1 Answers1

1

the issue come from the fact that each value in your $ordendetalle array is already encoded with json_encode so you can do:

$ordendetalle=array_map('json_decode',$ordendetalle);
$customer = array ( 'customer' => array ( 'id' => $ordendetalle['customerId'], 
                                          'lastname' => $lastname, 
                                          'firstname' => $ordendetalle['firstname'],
                                          'email' => $ordendetalle['email']
                                        )
                  );

$customer_order = print_r(json_encode($customer), true); //Print in a log
Elementary
  • 1,443
  • 1
  • 7
  • 17
  • After add $ordendetalle=array_map('json_decode',$ordendetalle); string values became null. "lastname": null, "firstname": null, "email": null – Exe Aug 07 '18 at 08:34