日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

字符串大写转小写库函数_PHP程序无需使用库函数即可将字符串转换为大写

發布時間:2025/3/11 php 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符串大写转小写库函数_PHP程序无需使用库函数即可将字符串转换为大写 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

字符串大寫轉小寫庫函數

Given a string and we have to convert it into uppercase string without using any library function.

給定一個字符串,我們必須在不使用任何庫函數的情況下將其轉換為大寫字符串。

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 )轉換為字符數組( $ chars ),然后使用ord()函數計算其ASCII值 。 由于我們知道在ASCII中,大寫字符恰好在小寫字母之前32位,因此我們從ASCII值減去32,然后使用chr()函數將其轉換回字符 。 輸出存儲在$ result變量中。

This program is a good proof of concept.

該程序是概念的很好證明。

翻譯自: https://www.includehelp.com/php/convert-string-to-uppercase-without-using-the-library-function.aspx

字符串大寫轉小寫庫函數

總結

以上是生活随笔為你收集整理的字符串大写转小写库函数_PHP程序无需使用库函数即可将字符串转换为大写的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。