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

字符串转换整数的库函数

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

字符串转换整数的库函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值