• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP str_replace

php 搞代码 3年前 (2022-01-23) 18次浏览 已收录 0个评论

这样能不转换字符串首字母, 意思是转换首字母大写。

function upper($text){$str_from = array ("a", "b", "c", "d", "i", "f");$str_to = array ("A", "B", "C", "D", "I", "F");$text = str_replace($str_from, $str_to, $text);return $text;echo upper(‘abcd’);

4本文¥来源gao!%daima.com搞$代*!码$网9

搞代gaodaima码

回复讨论(解决方案)

php 提供有函数 ucfirst

echo ucfirst ('abcd');

Abcd

自己写要用正则

echo preg_replace('/\b[a-z]/e', 'strtoupper("$0")', 'abcd');//或echo preg_replace_callback('/\b[a-z]/', function($m) { return strtoupper($m[0]); }, 'abcd');

ucfirst ? 将字符串的首字母转换为大写

不明白你弄两个数组想干嘛。

function upper($text){//$str_from = array ("a", "b", "c", "d", "i", "f");//$str_to = array ("A", "B", "C", "D", "I", "F");$first=strtoupper(substr($text,0,1));$str=substr($text,1);//$text = str_replace($str_from, $str_to, $text);return $first.$str;} echo upper('abcd');
Abcd

谢谢您们,
还有问题我不想转换strtoupper,
我想西里尔字母直接能不转换。

function upper($text){$str_from = array ("а", "?", "б", "в", "г", "?", "д");$str_to = array ("А", "?", "Б", "В", "Г", "?", "Д");$text = str_replace($str_from, $str_to, $text);return $text; echo upper(‘Дисьма’);

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP str_replace
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址