A. Cedano
A. Cedano

Reputation: 989

Transliterator for removing accents and other custom replacements

I'm trying to write a Transliterator that removes accents from letters and also changes commas, spaces and colons to - and periods to _.

This is what I've been trying, but it doesn't work, the object is NULL.

$transL =Transliterator::createFromRules('
    :: Any-Latin;
    :: Latin-ASCII;
    :: NFD;
    :: [:Nonspacing Mark:] Remove;
    :: NFC;
    :: [, ] > "-";
    :: [\u0020] > "-";
    :: [:Colon:] > "-";
    :: [. ] > "_";
',Transliterator::FORWARD);

var_dump($transL); # NULL

Upvotes: 1

Views: 129

Answers (1)

0stone0
0stone0

Reputation: 44274

You can use the intl_get_error_message() function to get the error causing NULL:

transliterator_create_from_rules: unable to create ICU transliterator from rules (parse error at offset 111, after ";
   :: [, ] >", before or at " "-";
   :: [\"): U_UNQUOTED_SPECIAL

Seems like it's caused by "-", replacing with an escaped - fixed that: :: [, ] > \-;.

<?php

$transL =Transliterator::createFromRules('
    :: Any-Latin;
    :: Latin-ASCII;
    :: NFD;
    :: [:Nonspacing Mark:] Remove;
    :: NFC;
    :: [, ] > \-;
    :: [\u0020}] > \-;
    :: [. ] > \_;
',Transliterator::FORWARD);

if (($error = intl_get_error_message()) === 'U_ZERO_ERROR') {
    $test = 'Foo,Bar';
    var_dump($transL->transliterate($test));
} else {
    var_dump($error);
}

Upvotes: 3

Related Questions