Not sure where to log this, and in the end perhaps it turns out not a bug, but it is so puzzling to me I need support.

I am calling preg_match() with the following pattern: '/^([A-ZÅÄÖ][\wÅÄÖåäö-]+)[\s]([A-ZÅÄÖ][\wåäöÅÄÖ-]+)$/' and the string in the example below is 'Påijljrtan Öårtanu'.

The preg_match is supposed to allow (Swedish) names on form First Last with space in between and allowing characters such as åäö, very common in Swedish. The input is taken from a form '$form_state['values']['title']'.

Now, I called preg_match from a #validator hook (using the workaround requied in D6 to reach _validate function and be able to access '$form_state['values']['title']'. I was surprised to find that the preg_match did not match unless I took out all special characters. After hours of fruitless debugging (and learning some things about character sets and encoding...), I tried the same thing but validating in the validator_api module instead. IT WORKED!

Now I have no idea why, I supply some printouts from both cases below (using drupal_set_message()) and I can NOT figure out why the difference. In the trace below the same code is executed sequentially with the same input, first the validator api, then the #validator hook. I hope it is clear. You will see the output from preg_match (0 or 1) is output at the end of each trace output, just to show when it is working and when not.

PS. By the way I'm not claiming that Påijljrtan Öårtanu is a common name in Sweden ;-)

Code in my custom module validate hook:
-----------------------------------------
$reg = preg_match('/^([A-ZÅÄÖ][\wÅÄÖåäö-]+)[\s]([A-ZÅÄÖ][\wåäöÅÄÖ-]+)$/', $form_state['values']['title']);
drupal_set_message('Trace (drupal_set_message()) in #validate hook with raw output of variable $form_state[\'values\'][\'title\']:'.$form_state['values']['title'].'.'.$reg, '');
drupal_set_message('Trace in #validate hook: utf8_encode()result: '.utf8_encode($form_state['values']['title']).'.'.$reg, '');
drupal_set_message('Trace in #validate hook: iconv()result: '.iconv( "UTF-8", "ISO-8859-1//TRANSLIT", $form_state['values']['title']).'.'.$reg, '');
drupal_set_message('Trace in #validate hook: mb_detect_encoding() result:'.mb_detect_encoding($form_state['values']['title']).'.'.$reg, '');

if ($reg == 0) {
form_set_error('title',t('Please enter analyst name on the form \'First Last\'. Minimum 2 characters per name. Separate double names with a dash (-), only one space and no other special characters allowed.'));
return;
}

validator api validator code:
----------------------------
$reg = preg_match('/^([A-ZÅÄÖ][\wÅÄÖåäö-]+)[\s]([A-ZÅÄÖ][\wåäöÅÄÖ-]+)$/', $form_state['values']['title']);
drupal_set_message('Trace (drupal_set_message()) in validator api with raw output of variable $form_state[\'values\'][\'title\']:'.$form_state['values']['title'].'.'.$reg, '');
drupal_set_message('Trace in validator api: utf8_encode()result: '.utf8_encode($form_state['values']['title']).'.'.$reg, '');
drupal_set_message('Trace in validator api: iconv()result: '.iconv( "UTF-8", "ISO-8859-1//TRANSLIT", $form_state['values']['title']).'.'.$reg, '');
drupal_set_message('Trace in validator api: mb_detect_encoding() result:'.mb_detect_encoding($form_state['values']['title']).'.'.$reg, '');
if ($reg == 0) {
form_set_error('title',t('Please enter analyst name on the form \'First Last\'. Minimum 2 characters per name. Separate double names with a dash (-), only one space and no other special characters allowed.'));
return false;
}
return true;

TRACE OUTPUT:
----------------
Trace (drupal_set_message()) in validator api with raw output of variable $form_state['values']['title']:Påijljrtan Öårtanu.1
Trace in validator api: utf8_encode()result: Påijljrtan Öårtanu.1
Trace in validator api: iconv()result: P�ijljrtan ��rtanu.1
Trace in validator api: mb_detect_encoding() result:UTF-8.1
Trace (drupal_set_message()) in #validate hook with raw output of variable $form_state['values']['title']:Påijljrtan Öårtanu.0
Trace in #validate hook: utf8_encode()result: Påijljrtan Öårtanu.0
Trace in #validate hook: iconv()result: P�ijljrtan ��rtanu.0
Trace in #validate hook: mb_detect_encoding() result:UTF-8.0

BR
//Per

Comments

magic72’s picture

Category: support » bug

Changing this to Bug report since there seems to be no feedback to point in another direction.

Status: Active » Closed (outdated)

Automatically closed because Drupal 6 is no longer supported. If the issue verifiably applies to later versions, please reopen with details and update the version.