Home > Backend Development > PHP Tutorial > PHP str_replace

PHP str_replace

WBOY
Release: 2016-06-23 13:48:42
Original
965 people have browsed it

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

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’);
Copy after login


回复讨论(解决方案)

php 提供有函数 ucfirst

echo ucfirst ('abcd');
Copy after login
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');
Copy after login

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');
Copy after login


Abcd
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template