php字符串反转函数_PHP | 反转给定的字符串而不使用库函数

php字符串反转函数

Given a string and we have to reverse it without using a library function.

给定一个字符串,我们必须不使用库函数而将其反转。

Example:

例:

    Input: "Hello world!"
Output: "!dlrow olleH"
Input: "Welcome @ IncludeHelp.Com"
Output: "moC.pleHedulcnI @ emocleW"

PHP代码无需使用库函数即可反转字符串 (PHP code to reverse the string without using library function)

<?php
//PHP code to reverse the string without 
//using library function
//function definition 
//it accepts a string and returns the revrse string
function reverse_string($text){
$rev = ''; //variable to store reverse string
$i = 0; //counting length
//calculating the length of the string 
while(isset($text[$i])){
$i++;
}
//accessing the element from the reverse
//and, assigning them to the $rev variable 
for($j = $i - 1; $j >= 0; $j--){
$rev .= $text[$j];
}    
//returninig the reversed string
return $rev;
}
//main code i.e. function calling
$str = "Hello world!";
$r_str = reverse_string($str);
echo "string is: ". $str . "<br/>";
echo "reversed string is: ". $r_str . "<br/>";
$str = "Welcome @ IncludeHelp.Com";
$r_str = reverse_string($str);
echo "string is: ". $str . "<br/>";
echo "reversed string is: ". $r_str . "<br/>";
?>

Output

输出量

string is: Hello world!
reversed string is: !dlrow olleH
string is: Welcome @ IncludeHelp.Com
reversed string is: moC.pleHedulcnI @ emocleW

Explanation:

说明:

Since we can't use the library function, In the function - we run a for loop to reverse the strings by storing the sequence in reverse order in the variable $rev. An additional while loop is set up to check if the variable $text contains a valid string (i.e. to calculate the length). This is an additional safety check to ensure that the program works even if numbers are put into the function.

由于无法使用库函数,因此在函数中,我们运行一个for循环,以相反的顺序将序列存储在变量$ rev中,以反转字符串。 设置了一个附加的while循环,以检查变量$ text是否包含有效的字符串(即,计算长度)。 这是一项附加的安全检查,以确保即使在功能中输入了数字,程序也可以正常运行。

翻译自: https://www.includehelp.com/php/reverse-a-given-string-without-using-the-library-function.aspx

php字符串反转函数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380293.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

boost::regex学习(2)

四&#xff1a;regex_match例子代码学习1 我们经常会看一个字符串是不是合法的IP地址&#xff0c;合法的IP地址需要符合以下这个特征&#xff1a;xxx.xxx.xxx.xxx 其中xxx是不超过255的整数正则表达式找到上面的这种形式的字符串相当容易&#xff0c;只是判断xxx是否超过255就比…

C++ 随机数

#include < iostream> #include < ctime> #include < cstdlib> using namespace std; int main () { int i,j; // 设置种子 srand( (unsigned)time( NULL ) ); /* 生成 10 个随机数 */ for( i 0; i < 10; i ) { // 生成实际的随机数 j rand…

远控免杀4---Evasion免杀

0x01 免杀能力一览表 1、下表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/meterperter/reverse_tcp模块生成。3、由于本机测试时只是安装了360全家桶和火绒&#xff0c;所以…

微内核和宏内核的区别_8086微处理器中的过程和宏之间的区别

微内核和宏内核的区别Prerequisite 先决条件 Procedure in 8086 Microprocessor 8086微处理器中的过程 Macros in 8086 Microprocessor 8086微处理器中的宏 过程和宏之间的区别 (Differences between Procedures and Macros ) CharacteristicProcedureMacroNumber of Instruct…

对决

描述 Topcoder 招进来了 n 个新同学&#xff0c;Yougth计划把这个n个同学分成两组&#xff0c;要求每组中每个人必须跟另一组中每个同学进行一次算法对决&#xff0c;问存不存在一种分组方式在k场完成对决。&#xff08;两组中每一组中人数都要大于0&#xff09; //注意&…

我的世界方块云服务器bug,我的世界:两个方块能无限刷经验?这装置太BUG了

在我的世界中&#xff0c;刷经验是一种很常见的事情&#xff0c;不过以往的刷经验机&#xff0c;常常需要建造刷怪塔刷怪&#xff0c;不仅麻烦&#xff0c;工程量大&#xff0c;怪物积累多了还会造成卡顿&#xff0c;非常不方便。但是随着我的世界更新了火炉这种物品之后&#…

远控免杀5---Veil免杀

0x01 免杀能力一览表 1、下表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/meterperter/reverse_tcp模块生成。3、由于本机测试时只是安装了360全家桶和火绒&#xff0c;所以…

ADO.NET_09_Using 关键字

本文内容 演示 Using 关键字验证 Using 关键字演示 Using 关键字 string connStr "data sourceora11; uidscott; pwdtiger; unicodetrue"; string sqlStr "SELECT * FROM EMP"; using (OracleConnection conn new OracleConnection(connStr)) { conn.Op…

js isinteger_在JavaScript中使用示例使用Number isInteger()方法

js isinteger编号isInteger()方法 (Number isInteger() Method) isInteger() is a Number Method, it is used to check whether a given number is an integer or not. isInteger()是一个数字方法&#xff0c;用于检查给定数字是否为整数。 It returns true if given number …

最新:日文.NET Framework 3.5 与 VS 2008 beta 2 已经公布

半个月以前,微软发布了Beta2版本的.NET Framework 3.5 与 Visual Studio 2008,可以从这里下载.而本周,代表双字节字符区域的第一个日文版本Beta2已经发布,现在同样也可以提供下载,这意味着中文版将在不久后与我们见面(按照微软软件的惯例,除微软中国研究院自主推出的软件外,中文…

三角形描边css,[CSS] tips带有描边的小箭头

linear-gradient和border:Document.box {position: relative;padding: 10px; /* 重要 防止内容被覆盖 */text-align: center;border: 1px solid #f60;border-radius: 5px;}.box::after {content: ;position: absolute;left: 50%;display: table;width: 10px;height: 10px;margi…

sql语句中出现笛卡尔乘积

没有join条件导致笛卡尔乘积 学过线性代数的人都知道&#xff0c;笛卡尔乘积通俗的说&#xff0c;就是两个集合中的每一个成员&#xff0c;都与对方集合中的任意一个成员有关联。可以想象&#xff0c;在SQL查询中&#xff0c;如果对两张表join查询而没有join条件时&#xff0c;…

远控免杀专题6---Venom免杀

0x01 免杀能力一览表 几点说明&#xff1a; 1、上表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。 2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/meterperter/reverse_tcp模块生成。 3、由于本机测试时只是安装了360全…

人工智能 自然语言处理_自然语言处理(NLP)| 人工智能

人工智能 自然语言处理Natural Language Processing (NLP) is one of the most successful applications of Artificial Intelligence in the real world. The Natural Language Processing in the terms of AI means that the Artificial Agents are able to able to understa…

精挑细选

描述 小王是公司的仓库管理员&#xff0c;一天&#xff0c;他接到了这样一个任务&#xff1a;从仓库中找出一根钢管。这听起来不算什么&#xff0c;但是这根钢管的要求可真是让他犯难了&#xff0c;要求如下&#xff1a; 1、 这根钢管一定要是仓库中最长的&#xff1b; 2、 这根…

hoho,我要坚持写blog

争取一天一篇,提高技术. 转载于:https://www.cnblogs.com/lbq1221119/archive/2007/08/17/859183.html

查看ajax传来的数据,jQuery AJAX 方法 success()后台传来的4种数据

1.后台返回一个页面js代码/**(1)用$("#content-wrapper").html(data);显示页面*/$.ajax({async : false,cache : false,type : POST,url : area/prepareCreate,error : function() {alert(smx失败 );},success : function(data) {$("#content-wrapper").ht…

远控免杀专题7 ---shellter免杀

0x01 免杀能力一览表 几点说明&#xff1a; 1、上表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。 2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/meterperter/reverse_tcp模块生成。 3、由于本机测试时只是安装了360全…

Notepad++高亮AS文件

如果编写ActionScript&#xff0c;可以选择语言->Flash actionScript 但是软件似乎不能确定as文件使用的语言&#xff0c;需要修改软件配置 Notepad 有 ActionScript 的语法高亮功能, 但它似乎不能被程序探查到. 打开的每个 AS 文件, 都要自行套用语法高亮, 特别麻烦. 当你打…

python 示例_Python日历类| yeardayscalendar()方法与示例

python 示例Python Calendar.yeardayscalendar()方法 (Python Calendar.yeardayscalendar() Method) Calendar.yeardayscalendar() method is an inbuilt method of the Calendar class of calendar module in Python. It uses an instance of this class and returns the list…