PHP:json_encode dilema with strings containing invalid UTF8? is it ok to utf8_encode each string? -


this json_encode dropping/nullifying values contain non standard utf8 string accented characters.

i've read many different solutions this, , conceptualized creating own function automatically me blanket solution.

by way, expect plain english strings.

basically want function replace json_encode , 1 have routine correct string/objects passed before passing actual json_encode function.

so question is, what's best routine this? , best, takes account practical , efficient.

for example, i've created routine break each character in string , evaluates if valid utf8, if not encodes it.. (this exampled 1 of users @ php.net

this character character check , fix w/c seems bullet proof.

but problem impact performance.

so way run utf8_encode on each string passed.. i'm thinking checking each value / string if contains invalid utf8 characters first, before running utf8_encode() on save on overhead.. again , each "check" routine overhead in itself..

so checking first necessary? if run utf8_encode() on string passed through function regardless of string needing utf8 corrections?

is utf8_encode() lightweight function? have significant performance impact if run hundreds of strings through utf8_encode()? (like templating engine have using json output)

looking solid advises , feedbacks.

regards

----update----

so injected array walk recursive function go through objects , utf8_encode of it..

enter image description here

$response multidimensional array containing around 1000 nodes:

$response['stat']="ok"; $response['contacts'][0]['name']="brad"; $response['contacts'][0]['email']="brad@domain.com"; $response['contacts'][0]['number']="1800-55850"; $response['contacts'][1]['name']="johj"; $response['contacts'][1]['email']="johj@domain.com"; $response['contacts'][1]['number']="1800-7777"; 

...and on..

my script time results follows:

w/o utf8_encode hammering - 1st run: 0.86414098739624

  • 2nd run: 0.86342883110046

  • 3rd run: 0.88974404335022

wtih utf8_encode hammering - 1st run: 0.91330289840698

  • 2nd run: 0.90936899185181

  • 3rd run: 0.89101815223694

about 100ms trade of..hmmm


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -