简体   繁体   中英

Capitalize the first letter of sentences in paragraphs which are in turkish language

I am trying to create a simple php function to capitalize only the first letter of every sentence in a paragraph. The code works but I have problems with turkish characters.

$string = "YAĞMUR YAĞIYORDU. ŞEMSİYESİNİ ÇIKARDI"; //Example sentence

$string = ucfirst($string);

$string = preg_replace_callback('/[.!?] .*?\w/',
          create_function('$matches', 'return strtoupper($matches[0]);'),
          $string);

This may work for you

 $str= "YAĞMUR YAĞIYORDU. ŞEMSİYESİNİ 
 ÇIKARDI"; //Example sentence

function my_mb_ucfirst($str) {
   $fc = mb_strtoupper(mb_substr($str, 0, 1));
   return $fc.mb_substr($str, 1);
}

 echo  my_mb_ucfirst($str);

Edit:

function ucfirst_turkish($str) {
  $tmp = preg_split("//u", $str, 2,    
 PREG_SPLIT_NO_EMPTY);
 return mb_convert_case(
    str_replace("i", "İ", $tmp[0]),     
  MB_CASE_TITLE, "UTF-8").
    $tmp[1];
}

$str= "YAĞMUR YAĞIYORDU. ŞEMSİYESİNİ 
 ÇIKARDI"; //Example sentence

echo ucfirst($str) ."\n";   
echo ucfirst_turkish($str); 

NB: If it doesn't work then check some example for Turkish language here http://php.net/manual/en/function.ucfirst.php

okay, this is hideous, but it works... at least for the string you provided.

i could not get your preg_match to get that 'ş' after the period no matter how i fiddled, and replacing strtoupper() with mb_convert_case in your original example didn't do the trick either.

so, i've refactored it into a hideous loop over the chars, testing for periods as sentence-enders.

<?php
// the string. i've made it lower case here to make the test simpler
$string = "yağmur yağiyordu. şemsiyesini çikardi"; //example sentence

// multibyte-safe splitting of the string into an array of chars. note that php arrays are by default one byte
// so simply accessing $string[3] may not give you the char you think.
$bodyArray = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);

// us mb_convert_case to get the first char  of the sentence. you may wish to do some trimming here to 
// confirm that it's not a space..
$bodyArray[0] = mb_convert_case($bodyArray[0],MB_CASE_UPPER);

// the buffer to hold your capitalized string
$buffer  = "";

// each char
for($i=0;$i<count($bodyArray);$i++) {

    // if previous char was a period and the current char is not a space, uppercase the char
    if($ucflag && $bodyArray[$i] != " ") {
        $bodyArray[$i] = mb_convert_case($bodyArray[$i],MB_CASE_UPPER);
        $ucflag = false;
    }

    // if this char is a period, set a flag to uppercase the next non-space char
    if($bodyArray[$i] == ".") {
        $ucflag = true;
    }

    // add the char to the buffer
    $buffer .= $bodyArray[$i];
}

print $buffer;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM