字符串大写转小写库函数_PHP程序无需使用库函数即可将字符串转换为大写
字符串大寫轉(zhuǎn)小寫庫函數(shù)
Given a string and we have to convert it into uppercase string without using any library function.
給定一個字符串,我們必須在不使用任何庫函數(shù)的情況下將其轉(zhuǎn)換為大寫字符串。
PHP code:
PHP代碼:
<?php //function definition //this function accepts a string/text, converts //text to uppercase and return the uppercase converted string function upperCase($str) {$chars = str_split($str);$result = '';//loop from 0th character to the last characterfor ($i = 0; $i < count($chars); $i++) {//extracting the character and getting its ASCII value$ch = ord($chars[$i]);//if character is a lowercase alphabet then converting //it into an uppercase alphabetif ($chars[$i] >= 'a' && $chars[$i] <= 'z')$result .= chr($ch - 32);else$result .= $chars[$i];}//finally, returning the stringreturn $result; }//function calling $text = "hello world"; echo upperCase($text); echo "<br>";$text = "Hello world!"; echo upperCase($text); echo "<br>";$text = "[email?protected]"; echo upperCase($text); echo "<br>";?>Output
輸出量
HELLO WORLD HELLO WORLD! [email?protected]Code explanation:
代碼說明:
We convert the string ($str) into an array of characters ($chars) then calculate their ASCII value using ord() function. Since we know that in ASCII, the Upper Case characters come exactly 32 places before the lower case equivalent, we subtract 32 from the ASCII value and then convert it back to the character using the chr() function. The output is stored in the $result variable.
我們將字符串( $ str )轉(zhuǎn)換為字符數(shù)組( $ chars ),然后使用ord()函數(shù)計算其ASCII值 。 由于我們知道在ASCII中,大寫字符恰好在小寫字母之前32位,因此我們從ASCII值減去32,然后使用chr()函數(shù)將其轉(zhuǎn)換回字符 。 輸出存儲在$ result變量中。
This program is a good proof of concept.
該程序是概念的很好證明。
翻譯自: https://www.includehelp.com/php/convert-string-to-uppercase-without-using-the-library-function.aspx
字符串大寫轉(zhuǎn)小寫庫函數(shù)
總結(jié)
以上是生活随笔為你收集整理的字符串大写转小写库函数_PHP程序无需使用库函数即可将字符串转换为大写的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ai推理_人工智能推理能力问答
- 下一篇: SpringBoot官方热部署和远程调试