簡體   English   中英

大寫土耳其語段落中句子的首字母

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

我正在嘗試創建一個簡單的php函數,以僅將段落中每個句子的首字母大寫。 該代碼有效,但是我遇到了土耳其字符問題。

$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);

這可能對你有用

 $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);

編輯:

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); 

注意:如果不起作用,請在此處查看一些土耳其語示例, 網址http://php.net/manual/zh/function.ucfirst.php

好的,這很可怕,但它至少對您提供的字符串有效。

無論我如何擺弄,我都無法在周期后獲得preg_match來獲得“ş”,並且在原始示例中用mb_convert_case替換strtoupper()也不起作用。

因此,我將其重構為字符上的一個可怕的循環,測試了作為句子提供者的時間段。

<?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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM