Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2017-09-29 01:18:59

david@druna.cz
Member
Registered: 2017-07-25
Posts: 41

Cannot process strings from custom fields in PHP (messed up encoding?)

I wanted to define a JSON configuration structure for each article to use afterwards in php so I used the glz_custom_fields plugin for the multi-line custom field input on the article page. Now when I enter {"ac":"by"} (and nothing else) into that field and then get it from inside a form from php and echo it, it displays alright, but the json parsing function (json_decode) always returns NULL with the error code 4 which stands for JSON_ERROR_SYNTAX.

The multibyte functions also show there is something wrong. For instance mb_strlen($abc, 'UTF-8') returns 31 for the said string.

I meesed up with mb_substr( $abc , X , Y, 'UTF-8' ) within the interval (0, 31) trying to isolate characters in case there are some invisible characters being added but the output is visible and garbled.

When I set the exactly same string right in the php file, the mb_strlen function returns the expected number and the json parses the string properly.

I have tried retrieving the custom field in two ways in case there is a difference, but both ways behave the same:

$abc = custom_field(array('name' => 'custom field name'));
$abc = parse('<txp:custom_field name="custom field name" />');

The database says it uses UTF-8:

MySQL charset: UTF-8 Unicode (utf8)
MySQL connection collation: utf8-unicode-ci

The Textpattern diagnostics seem to suggest TXP is using UTF-8 too:

Charset (default/config): latin1/utf8
character_set_client: utf8
character_set_connection: utf8
character_set_database: utf8
character_set_filesystem: binary
character_set_results: utf8
character_set_server: utf8
character_set_system: utf8
character_sets_dir: /usr/share/mysql/charsets/
17 Tables: OK

Please help

Last edited by david@druna.cz (2017-09-29 12:01:26)

Offline

#2 2017-09-29 01:48:31

david@druna.cz
Member
Registered: 2017-07-25
Posts: 41

Re: Cannot process strings from custom fields in PHP (messed up encoding?)

Now i have a suspicion that textile gets applied and screws the string (although I have textile temporarily turned off for the article). A single double quote gets into the php as (in byte representation): 38 113 117 111 116 59 (6 bytes)

Last edited by david@druna.cz (2017-09-29 01:48:45)

Offline

#3 2017-09-29 01:54:13

david@druna.cz
Member
Registered: 2017-07-25
Posts: 41

Re: Cannot process strings from custom fields in PHP (messed up encoding?)

Uaaah t seems right. When i enter the text in the excerpt which has textile disabled thebyte representation matches the on of the string defined in php. (I really have the experience of suddenly getting closer to solving things once I post a question online :-) So now I guess it is more of a question how to disable textile for custom fields.

Offline

#4 2017-09-29 11:27:33

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,304

Re: Cannot process strings from custom fields in PHP (messed up encoding?)

david@druna.cz wrote #307252:

I really have the experience of suddenly getting closer to solving things once I post a question online :-)

We all have :)

So now I guess it is more of a question how to disable textile for custom fields.

Is this topic still relevant, then? If not, please mark it as [SOLVED] at the beginning of the topic title. Thanks.
If it’s still relevant: Without Textile’s code marker “@” the forum’s Textile probably swallows parts of your original {"ac":"by"}.


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#5 2017-09-29 11:55:20

david@druna.cz
Member
Registered: 2017-07-25
Posts: 41

Re: Cannot process strings from custom fields in PHP (messed up encoding?)

Actually it seems it still is. When I compare the byte output of the single double quoute from the custom field it is different from the same double quoute taken from te excerpt with textile turned on. I really don’t know what’s happening.

Offline

#6 2017-09-29 18:46:42

etc
Developer
Registered: 2010-11-11
Posts: 5,057
Website GitHub

Re: Cannot process strings from custom fields in PHP (messed up encoding?)

Custom fields are HTML-encoded by default, try

<txp:custom_field name="myfield" escape="" />

Offline

Board footer

Powered by FluxBB