Drupal: node_save and special characters
Submitted by tanay on Thu, 05/03/2012 - 06:50
- Trying to create nodes or taxonomy terms programatically?
- Have characters like "é", "è" or "à" in the title or body or any of the fields?
- Struck with database errors while doing node_save ?
- Is the text being truncated after the first occurrence of such a character?
Stop pulling out your hair on these encoding/charset problem...
With Drupal, everything is encoded (and expected) in the UTF-8 charset.
- Make sure that everything in your application is working with/in UTF-8. Your PHP files, containing your source-code, should be in UTF-8 There is probably some setting about that in your IDE/editor This will ensure that the strings you hard-code are in UTF-8 too
- When using some external data, before calling node_save on it, you must make sure it's in UTF-8. The utf8_encode function should work. Or maybe mb_convert_encoding, depending on the charset used by your input data.
In a nutshell, instead of
$node->field_tm_bft_bil_ses[LANGUAGE_NONE]['value'] = $bill['BILLSESSION'];
$node->field_tm_bft_bil_ses[LANGUAGE_NONE]['value'] = utf8_encode($bill['BILLSESSION']);