首頁  >  文章  >  後端開發  >  PHP ucwords()

PHP ucwords()

王林
王林原創
2024-08-29 12:48:32898瀏覽

PHP 中的 ucwords() 是內建函數。將字串的第一個也是最重要的字元轉換為大寫是很有幫助的。 ucwords() 僅支援 PHP 4 以上版本。 ucwords() 函數將字串作為輸入值,並透過將字串的第一個字母/字元變更為大寫來輸出字串。除此之外,其他角色都與上次相同。 PHP 中的 ucwords() 函數透過將所有單字的首字母更改為大寫回傳轉換為字串。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

Ucwords($string, $separator)

ucwords() 語法只接受兩個參數。

語法參數:

1。 $string( string required ): ucwords() 函數的括號之間,需要輸入字串。為了指定要轉換的字串,此函數宣告是必須的。

2。 $separator ( 可選參數 ): Separator 是 ucwords() 函數的可選參數。它包含單字分隔符號。在單字的輸入字串中使用的分隔符號。下面列出的字元是預設的:

  • t 代表選項卡
  • 空間
  • r 表示回車
  • n 換行
  • v 表示垂直製表符
  • f 用於換頁

PHP 5.5.16、5.4.32 版本中新增了 $Separator 參數。

HPHP ucwords() 如何運作?

當文字/單字在單字中包含混合類型的字母/字元時,PHP ucwords 可以工作。只有單字的第一個字元/句子中所有單字的第一個字元才會轉換為大寫字母。它使用包含一個或多個單字的字串值,並且還使用一個分隔符號/定界符值,但它是可選的。分隔符號變數沒有問題。

PHP ucwords() 範例

以下是範例

範例#1

基本程式如何使用 ucwords() PHP 函數運作。

代碼:

<?php
$input_string = "hey buddy, pavan sake is coming just wait.";
echo "Before:". $input_string;
echo '</br>';
$result_string = ucwords($input_string);
echo "After: ".$result_string;
?>

輸出:

PHP ucwords()

範例#2

代碼:

<?php
$input_string = "guys|good|night!|everyone.";
$result_string1 = ucwords($input_string);
echo $result_string1. "</br>";
$result_string2 = ucwords($input_string, "|");
echo $result_string2;
?>

輸出:

PHP ucwords()

這裡的輸出$result_string1 提供了相同的整個字串,除了第一個大寫字母發生變化,但在使用分隔符號“|”之後參數, $result_string2 將根據我們的需要提供輸出- 字符串中的每個字母,即使在分隔符號之後,也會將其第一個字元變更為大寫字母。

範例 #3

這裡的範例是在陣列上使用 ucwords() 函數,該函數透過刪除分隔符號/參數「–」和「」來包含名稱/字串清單。

代碼:

<?php
//FUNCTION to implement ucwords() function on arrays which has list of names/strings
function ucname($string1) {
$string1 =ucwords(strtolower($string1));
foreach (array('-', '\'') as $parameters1) {
if (strpos($string1, $parameters1)!==false) {
$string1 =implode($parameters1, array_map('ucfirst', explode($parameters1, $string1)));
}
}
return $string1;
}
?>
<?php
//TEST
$names1 =array(
'SAKE-PAVAN KUMAR',
'ANIL O\'KUMAR',
'MARUTHI PRASAD',
'surendra la gandham',
'rAjAsEkHaR KAtUbaDi'
);
//Here in the $names1, you can add more strings into your array as per your requirement.
foreach ($names1 as $name1) { print ucname("{$name1}\n</br>"); }
//PRINTS:
/*
Sake-Pavan Kumar
Anil O'Kumar
Maruthi Prasad
Surendra La Gandham
Rajasekhar Kattubadi
*/
?>

輸出:

PHP ucwords()

範例#4

這是ucwords函數的範例程式之一。

程式具有以下功能:

  • 多位元組/位元組相容性
  • 即使有多個分隔符號也能處理

代碼:

<?php
function ucwords_specific1 ($string1, $delimiters1 = '', $encoding1 = NULL)
{
if ($encoding1 === NULL) { $encoding1 = mb_internal_encoding();}
if (is_string($delimiters1))
{
$delimiters1 =  str_split( str_replace(' ', '', $delimiters1));
}
$delimiters_pattern11 = array();
$delimiters_replace11 = array();
$delimiters_pattern21 = array();
$delimiters_replace21 = array();
foreach ($delimiters1 as $delimiter1)
{
$uniqid1 = uniqid();
$delimiters_pattern11[]   = '/'. preg_quote($delimiter1) .'/';
$delimiters_replace11[]   = $delimiter1.$uniqid1.' ';
$delimiters_pattern21[]   = '/'. preg_quote($delimiter1.$uniqid1.' ') .'/';
$delimiters_replace21[]   = $delimiter1;
}
// $return_string1 = mb_strtolower($string1, $encoding1);
$return_string1 = $string1;
$return_string1 = preg_replace($delimiters_pattern11, $delimiters_replace11, $return_string1);
$words1 = explode(' ', $return_string1);
foreach ($words1 as $index1 => $word1)
{
$words1[$index1] = mb_strtoupper(mb_substr($word1, 0, 1, $encoding1), $encoding1).mb_substr($word1, 1, mb_strlen($word1, $encoding1), $encoding1);
}
$return_string1 = implode(' ', $words1);
$return_string1 = preg_replace($delimiters_pattern21, $delimiters_replace21, $return_string1);
return $return_string1;
}
?>
<?php
mb_internal_encoding('UTF-8');
$string1 = "PAVAN KUMAR-SAKE d'alltechscience şŠ-òÀ-éÌ hello - web";
echo ucwords_specific1( mb_strtolower($string1, 'UTF-8'), "-'");
?>

輸出:

上述程式中涉及的主要參數有$string1、$delimeter1、$delimiters、encoding。 Delimeter/Delimeter 是一個選項,但在開發中需要的參數。字串是要轉換的參數。編碼參數是為了知道字元編碼。如果參數不省略,將使用內部字元編碼值。

PHP ucwords()

範例#5

代碼:

<?php
//This php syntax is to know how ucwords() function delivers the output if separator/parameter is not used.
$title1 = 'PAVAN "THE KING" SAKE - (I WANT TO BE YOUR) SERVANT'; //STRING declaration with strings
echo ucwords(strtolower($title1)); // here strtolower will convert $title to all small letters
// ucwords now will provides output like this:
// Pavan "the King" Sake - (i Want To Be Your) Servant
// so the below program makes it change to correct format i mean the king should be The King
?>
<?php
function my_ucwords($string1)
{
$noletters1='"([/'; //add some more elements if u like to add
for($i=0; $i<strlen($noletters1); $i++) //loop to access all the characters which is listed in $noletters1 variable
$string1 = str_replace($noletters1[$i], $noletters1[$i].' ', $string1);
$string1=ucwords($string1); //here ucwords() function will do the task of completing the first character of the words into capital letters
for($i=0; $i<strlen($noletters1); $i++)
$string1 = str_replace($noletters1[$i].' ', $noletters1[$i], $string1);
return $string1; //it will return the string value from the function.
}
echo '</br> </br>';
$title1 = 'PAVAN "THE KING" SAKE - (I WANT TO BE YOUR) SERVANT';
echo my_ucwords(strtolower($title1));
?>

輸出:

PHP ucwords()

範例 #6

這是下面的程式碼範例,它將把除第一個字母之外的所有單字轉換為小寫字母。它們將是大寫字母。這裡使用ucfirst()函數。它也是 ucwords() 函數的一部分。

代碼:

<?php
$text1 = "What Buddy ? No 'parameters',shit! \"happening\" here.this solves many problems now???";
preg_match_all('/[A-Za-z]+|[^A-Za-z]+/', $text1, $data1);
for ($i = 0; $i < count($data1[0]); $i++) {
$data1[0][$i] = ucfirst($data1[0][$i]);
}
$text1 = implode("", $data1[0]);
print $text1;
?>

輸出:

PHP ucwords()

上述程式的輸出包含 $text1 變數下的相同文本,但只有變數中列出的單字的第一個字元將更改為大寫字母,其餘的將保留為小寫字母。

以上是PHP ucwords()的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP ucfirst()下一篇:PHP ucfirst()