【问题标题】:Capitalize the first letter of sentences in paragraphs which are in turkish language土耳其语段落中句子的首字母大写
【发布时间】:2015-08-02 00:04:56
【问题描述】:

我正在尝试创建一个简单的 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);

【问题讨论】:

标签: php


【解决方案1】:

这可能对你有用

 $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/en/function.ucfirst.php

【讨论】:

  • 谢谢,但它会将句子中的所有单词大写。我只需要将每个句子的第一个字母大写
  • 很遗憾什么也没发生。
【解决方案2】:

好的,这很可怕,但它确实有效……至少对于您提供的字符串。

无论我如何摆弄,我都无法让你的 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-02
    • 2013-08-13
    • 2010-12-23
    • 2014-02-09
    • 2020-10-02
    • 2014-06-28
    • 2021-07-23
    • 2015-01-25
    相关资源
    最近更新 更多