I'm in PHP with my next problem. In a variable I receive the surname of the user but if the surname is for example, Hernández
the variable saves Hern\u00e1ndez
.
So that the last name I can later send it as it plays in a json, I am trying to do what I read in an English Stackoverflow answer:
$lastname_consumer = $ordendetalle['customer_lastname']; //recibo el apellido
$str_lastname = str_replace('\u','u',$lastname_consumer);
$lastname = preg_replace('/u([\da-fA-F]{4})/', '&#x;', $str_lastname);
$customer = array ( 'customer' => array ( 'id' => $ordendetalle['customerId'],
'lastname' => $lastname, //Hern\u00e1ndez
)
);
$customer_order = print_r(json_encode($customer), true); //Para pintarlo
My problem is that since first $str_lastname
is saving Hern\u00e1ndez
instead of Hernu00e1ndez
when doing str_replace()
, and nothing is worth to subsequently make the preg_replace()
and get Hernández
.
On the other hand, I've tried the code in link and it works perfectly for me but I understand that it's a matter of \u00e1
being a character < strong> UTF-8 . So I do not know very well how to advance at this point.
EDIT CLARIFICATION :
In my PHP I receive an object called $ordendetalle
that is generated in a part of the server where I do not have access ... $ordendetalle
is an array that contains, in fields string
, different values that have to be treated, including the surname:
$ordendetalle['customer_lastname'];//Hernu00e1ndez en vez de Hernández
I stress that the problem is at the time of:
$str_lastname = str_replace('\u','u',$lastname_consumer);
That does not replace \ u with the given character. Thanks for your time!