Drupal: node_save and special characters

  • 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.

  1. 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
  2. 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

->field_tm_bft_bil_ses[LANGUAGE_NONE][0]['value'] = $bill['BILLSESSION'];


->field_tm_bft_bil_ses[LANGUAGE_NONE][0]['value'] = utf8_encode($bill['BILLSESSION']);