在 PHP 中,可以使用以下方法來轉(zhuǎn)換字符串:
$str = 'Hello, World!';
echo $str; // 輸出 "Hello, World!"
$str1 = 'Hello';
$str2 = 'World!';
$result = $str1 . ', ' . $str2;
echo $result; // 輸出 "Hello, World!"
sprintf()
或 printf()
函數(shù):$str1 = 'Hello';
$str2 = 'World!';
$format = '%s, %s';
$result = sprintf($format, $str1, $str2);
echo $result; // 輸出 "Hello, World!"
// 或者
$str1 = 'Hello';
$str2 = 'World!';
$format = '%s, %s';
printf($format, $str1, $str2); // 輸出 "Hello, World!"
str_replace()
函數(shù)替換字符串中的某個部分:$str = 'Hello, World!';
$search = 'World';
$replace = 'PHP';
$result = str_replace($search, $replace, $str);
echo $result; // 輸出 "Hello, PHP!"
substr()
函數(shù)截取字符串中的某個部分:$str = 'Hello, World!';
$start = 0;
$length = 5;
$result = substr($str, $start, $length);
echo $result; // 輸出 "Hello"
ucwords()
函數(shù)將字符串中每個單詞的首字母轉(zhuǎn)換為大寫:$str = 'hello world';
$result = ucwords($str);
echo $result; // 輸出 "Hello World"
strtolower()
和 strtoupper()
函數(shù)將字符串轉(zhuǎn)換為小寫或大寫:$str = 'Hello, World!';
$lowercase = strtolower($str);
$uppercase = strtoupper($str);
echo $lowercase; // 輸出 "hello, world!"
echo $uppercase; // 輸出 "HELLO, WORLD!"
這些方法可以根據(jù)需要選擇使用,以實現(xiàn)字符串的轉(zhuǎn)換。