0

I am using foreign accented chars with url_title() in Codeigniter

function url_title ($str,$separator='-',$lowercase=FALSE) {
  if ($separator=='dash') $separator = '-';
  else if ($separator=='underscore') $separator = '_';
  $q_separator = preg_quote($separator);
  $trans = array(
    '\.'=>$separator,
    '\_'=>$separator,
    '&.+?;'=>'',
    '[^a-z0-9 _-]'=>'',
    '\s+'=>$separator,
    '('.$q_separator.')+'=>$separator
  );
  $str = strip_tags($str);
  foreach ($trans as $key => $val) $str = preg_replace("#".$key."#i", $val, $str);
  if ($lowercase === TRUE) $str = strtolower($str);
  return trim($str, $separator);
}

And I am calling the function as url_title(convert_accented_characters($str),TRUE);.

$str is being populated as:

$posted_file_full_name    = $_FILES['userfile']['name'];
$uploaded_file->filename = pathinfo($posted_file_full_name, PATHINFO_FILENAME);
$uploaded_file->extension = pathinfo($posted_file_full_name, PATHINFO_EXTENSION);

It works nicely UNLESS string start with a foreign character like Ç,Ş,Ğ. If those character are in the middle of the string, it converts gracefully. But if begins with those, it just removes the characters instead of replacing with mached ones.

Thanks for any help.

YahyaE
  • 1,057
  • 1
  • 9
  • 24
  • Try adding the `u` modifier to your regex. ([Pattern Modifiers ¶](http://php.net/manual/en/reference.pcre.pattern.modifiers.php)) – Gras Double Oct 13 '14 at 23:41

1 Answers1

0

After a tedious searching, it comes out that url_title() function is not the main reason. Actually, it's not the CI that removes initial foreign characters:

pathinfo($posted_file_full_name, PATHINFO_FILENAME);

This part removes initial characters. I updated my code as:

$uploaded_file->filename  = str_replace('.'.$uploaded_file->extension,'',$posted_file_full_name);

and now it works as expected. Hope this helps others who stucked in a such phase.

YahyaE
  • 1,057
  • 1
  • 9
  • 24
  • 1
    If it may help, from the [PHP documentation](http://php.net/manual/en/function.pathinfo.php#refsect1-function.pathinfo-notes): "pathinfo() is locale aware, so for it to parse a path containing multibyte characters correctly, the matching locale must be set using the setlocale() function." – Gras Double Oct 14 '14 at 22:45