字符串转换整数的库函数
Given a string and we have to convert it into lowercase string without using any library function.
给定一个字符串,我们必须在不使用任何库函数的情况下将其转换为小写字符串。
PHP code:
PHP代码:
<?php
//function definition
//this function accepts a string/text, converts
//text to lowercase and return the lowercase converted string
function lowercase($str)
{
$chars = str_split($str);
$result = '';
//loop from 0th character to the last character
for ($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 lowercase alphabet
if ($chars[$i] >= 'A' && $chars[$i] <= 'Z')
$result .= chr($ch + 32);
else
$result .= $chars[$i];
}
//finally, returning the string
return $result;
}
//function calling
$text = "HELLO WORLD";
echo lowercase($text);
echo "<br>";
$text = "Hello world!";
echo lowercase($text);
echo "<br>";
$text = "[email protected]";
echo lowercase($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 lowercase characters come exactly 32 places after the uppercase equivalent, we add 32 to 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-lowercase-without-using-the-library-function.aspx
字符串转换整数的库函数