Jump to content

How I record UTF-8 characters with the API?


hsanabria
 Share

Recommended Posts

My database uses UTF-8 and to create pages from the administrator with fields with special characters (eg é or ñ) are handled smoothly.

But in trying to create pages with the API, the values are cut when find the first special character.

For example:

$ p-> name = "Pedro Pérez"; // With é

or

$ p-> name = $ sanitizer-> text ("Peter Pérez"); // With é

Are recorded "Pedro P" and the rest are lost

Any suggestions to resolve the situation?

Link to comment
Share on other sites

Something similar happens to me when I search a sentence with special characters.

If the field is of type text or text area, the search no returns any coincidence, if I search a sentence with special characters.

If the field type is text area and this used CKeditor, they match and return the correct values.

Any idea why this functionality behaves like this?

Link to comment
Share on other sites

I think the "name" database column on pages is special, and your same approach to the title field of a page should work perfectly.

"name" field serves as URL alias, and therefore needn't contain special characters in order to not break URLs.

I'm not sure on the search issue. Maybe connected to the way MySQL searches?

Link to comment
Share on other sites

Hi Wanze:

Thanks for your time.

 
This is my PHP test code, where I think I applied your suggestion, but I get the same results.
 
<?php

header('Content-Type: text/html; charset=UTF-8');

$p = new Page();
$p->setOutputFormatting(false);
$p->template = 'cita'; // 
$p->parent = wire('pages')->get('/agenda/'); 
$p->name = $sanitizer->name("Solicitud de cita 20150629082201"); 
$p->title = $sanitizer->text("Solicitud de cita de Pedro Pérez");
$p->cita_nombre = $sanitizer->text("Pedro Pérez"); 
$p->cita_edad = 45;
$p->cita_telefono = "22334455";
$p->cita_correo = "pedro_perez@hotmail.com";
$p->cita_tipo = $sanitizer->text("Nueva cita");
$p->cita_otros_detalles = $sanitizer->textarea("Presión alta");
$p->cita_dia_hora1 = $sanitizer->text("Lunes 4:00 p.m.");
$p->cita_dia_hora2 = $sanitizer->text("Miércoles 5:00 p.m.");
$p->save();
echo "La página {$p->id} fue creada!<br>";

?>
All values are cut when find the first special character
 
Thanks in advanced
 
Henry
Link to comment
Share on other sites

I used the utf8_encode function and now the data is written correctly.

This is the modified test code, if is useful for someone.

<?php

header('Content-Type: text/html; charset=UTF-8');

$p = new Page();
$p->setOutputFormatting(false);
$p->template = 'cita'; // 
$p->parent = wire('pages')->get('/agenda/'); 
$p->name = $sanitizer->name("Solicitud de cita 20150629082201"); 
$p->title = $sanitizer->text(utf8_encode("Solicitud de cita de Pedro Pérez"));
$p->cita_nombre = $sanitizer->text(utf8_encode("Pedro Pérez")); 
$p->cita_edad = 45;
$p->cita_telefono = "22334455";
$p->cita_correo = "pedro_perez@hotmail.com";
$p->cita_tipo = $sanitizer->text(utf8_encode("Nueva cita"));
$p->cita_otros_detalles = $sanitizer->textarea(utf8_encode("Presión alta"));
$p->cita_dia_hora1 = $sanitizer->text(utf8_encode("Lunes 4:00 p.m."));
$p->cita_dia_hora2 = $sanitizer->text(utf8_encode("Miércoles 5:00 p.m."));
$p->save();
echo "La página {$p->id} fue creada!<br>";

?>

Thanks

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...