【问题标题】:PHP Capitalize first letter from the ALPHABET [closed]PHP将ALPHABET的第一个字母大写[关闭]
【发布时间】:2016-09-15 16:55:51
【问题描述】:

ucwords 和 ucfirst 将字符串的第一个字符转换为大写。

我需要一个函数来大写字母表中的第一个字符。

例如:¡ -> ¡你好! 'h' 应该是大写字母,因为它是字母表中的第一个字符。

在西班牙语中,我们在单词/句子/短语的开头和结尾都有问号,因此使用 ucword/ucfirst 将第一个字符大写并不能解决我的问题。我需要将字符串中的第一个字母字符大写。

¿你最喜欢的运动是什么? -> 在 ucfirst 之后 ->¿ 你最喜欢的运动是什么?

想要的结果:

¿你最喜欢的运动是什么?

//////-----------------/////// 更新//////------- -----------///////

感谢您的回答,我能够对其进行修改并获得最终功能。

这个函数将第一个字母大写(包括这个带重音的西班牙元音 á é í ó ú)

function _ucfirst($palabra) {
    $newStr = '';
    $match = 0;
    foreach(str_split($palabra) as $k=> $letter) {
        if($match == 0 && preg_match('/^\p{L}*$/', $letter)){
            $newStr .= _ucwords($letter);
            break;
        }else{
            $newStr .= $letter;
        }
    }
    return $newStr.substr($palabra,$k+1);
}

function _ucwords($palabra) {
    return mb_convert_case(mb_strtolower($palabra, 'iso-8859-1'), MB_CASE_TITLE, 'iso-8859-1');
}

【问题讨论】:

  • 再举一些例子和这样做的理由?为什么要这样做?你的尝试?
  • 可能先拆分单词,然后将字符串与非字母字符分开,然后执行 ucfirst 方法。
  • 也许:删除所有不是字母表中的字符并将第一个大写?
  • 这个问题很清楚,否则没人会回答。这只是一个简单的问题,以前没有人报告过(我搜索了很多问题)这个用例没有详细说明。标记我的问题是不公平的。在西班牙语中,我们在单词/句子/短语的开头和结尾都有问号,因此使用 ucword/ucfirst 将第一个字符大写并不能解决我的问题。我需要将字符串中的第一个字母字符大写
  • 我不认为mb_strtolower() 应该是mb_convert_case() 之前的必要条件。您的正则表达式模式表明您只有字母作为完整的输入字符串(甚至没有任何空格。这是真的吗?

标签: php uppercase capitalize


【解决方案1】:

无聊:

<?php

$str="!34hi Fred hi";
$match=0;//no match yet
$newStr='';//output string
foreach(str_split($str) as $letter){ //split string for loop

if($match==0 && ctype_alpha($letter)){//check its a letter and its the first one we found
    $newStr.=strtoupper($letter);//upper case it and glue it
    $match=1;// set the match so we don't bother to check any more of the string

}else{
    $newStr .=$letter; //glue the rest of the string
}


}

echo $newStr; //!34Hi Fred hi

不要用于长字符串,最好在找到第一个匹配项后中断循环,但对于不重要的短字符串。

长字符串版本,效率更高,在第一次匹配时停止循环:

<?php


$str="!34hi Fred hi";
$newStr='';//output string

foreach(str_split($str) as $k=> $letter){ //split string for loop

if($match==0 && ctype_alpha($letter)){//check its a letter and its the first one we found
    $newStr.=strtoupper($letter);//upper case it and glue it
    break;//stop the foreach on first find, no need to keep looping
}else{
    $newStr .=$letter; //glue the non letters so far found it any
}


}
//add the rest of the sting back in
echo $newStr.substr($str,$k+1); //!34Hi Fred hi

【讨论】:

  • 如果元音有重音符号,例如 á é í ó ú,ctype_alpha 不会返回 true
  • 如果元音有重音符号,例如 á é í ó ú,ctype_alpha 不会返回 true。这个可以吗? if(preg_match('/^\p{L}*$/',utf8_decode('holá') ) ) { echo 'is alpha'; }
  • @DIEGOF.G.如果您正确设置了setLocale(),那么ctype_alpha 将起作用。而且应该更快
【解决方案2】:

试试这个

  $a=strtolower("!34Hi Fred hi");
  $k=0;
  $b="";
  for($i=0;$i<strlen($a);$i++){
    if(ctype_alpha($a[$i])&&$k==0){
       $b.=strtoupper($a[$i]);
       $k=1;
    }else{
      $b.=$a[$i];
   }
}
echo $b;

【讨论】:

  • 使用 header('Content-type: text/html; charset=utf-8'); $a=strtolower("¡hi!");供您参考
  • 你为什么要复制我的代码?
  • 我没有复制你的代码,方法不同
  • 我使用了!34hi Fred hi,OP 使用了¡hi! ,所以我们俩决定使用!34hi Fred hi 绝非巧合。
猜你喜欢
  • 2010-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 2018-07-08
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
相关资源
最近更新 更多