php - str_replace


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.


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!

asked by Exe 06.08.2018 в 12:57

2 answers


In the end I have solved the doubt on the other hand, so I proceed to explain.

The problem is finally not solved by the str_replace , since by doing a var_dump($ordendetalle) we obtain.

["customer_id"]=> string(3) "979"
["customer_email"]=> string(23) "[email protected]"
["customer_firstname"]=> string(7) "UserTest"
["customer_lastname"]=> string(10) "Hernández"

That is, the surname is stored well inside the object $ordendetalle . The problem lies in the way that json encodes the " special " characters. json_encode preferably encodes non-ASCII characters using \u.... escape sequences.

To avoid that json_encode encode using these sequences we use JSON_UNESCAPED_UNICODE and would not need anything from str_replace or preg_replace , because when receiving Hern\u00e1ndez and doing json_encode with JSON_UNESCAPED_UNICODE we avoid using of the sequence \u00e1 and encode á with so you wanted to like this:

//$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' => $ordendetalle['lastname'], //Hern\u00e1ndez 

$customer_order = print_r(json_encode($customer,JSON_UNESCAPED_UNICODE), true);

And I would paint:

        "id": "979",
        "lastname": "Hernández"

A more theoretical explanation of JSON_UNESCAPED_UNICODE and where in the end I ended up solving the doubt you have it here

answered by 07.08.2018 / 13:31

Suppose that the endpoint where you ask the name gets an object in the answer and it is given to you by json_encoded, which is what the API should do if it is going to deliver a json and not a simple string:

$input = ['nombre'=>'joaquín', 'apellido'=>'hernández'];
$utf8_encoded = json_encode($input);

echo '<pre>';
echo '</pre>';

What you get, i.e. what the API delivers has the form:


You should be able to do:

echo 'str_lastname: '.$json_decoded['apellido'].PHP_EOL;

And get cleanly


But if you want to follow the path of the replacement:

$lastname_consumer = 'Hern\u00e1ndes';

$replaced =  str_replace("\u",'&',$lastname_consumer);
echo 'replaced: '.$replaced.PHP_EOL; // replaced: Hern&00e1ndes

$preg_replaced = preg_replace('/&([\da-fA-F]{4})/', '&#x;',$replaced); 
echo 'preg_replaced: '.$preg_replaced.PHP_EOL; // preg_replaced: Hernándes

// pero lo que en realidad contiene $preg_replaces es...    
$entities = htmlentities($preg_replaced);
echo 'entities: '.$entities.PHP_EOL; // entities: Hern&#x00e1;ndes

As you can see, $preg_replaced actually contains:


That is printed on the screen as Hernández because, badly wrong, PHP is a Hypertext Processor and by default it will print the HTML entity &#x00e1; as é .

The same could be achieved by doing:

$lastname_consumer = 'Hern\u00e1ndes';
$preg_replace = preg_replace('/\\u([\da-fA-F]{4})/', '&#x;', $lastname_consumer);

Depending on some configuration options and extensions, your replacement using regular expressions might fail or not deliver the expected results. That's why the first thing I suggested to you, using simply json_decode, seems to me the healthiest solution :

echo json_decode('"Hern\u00e1ndez"') // imprime Hernández
answered by 06.08.2018 в 14:55