PHP string learning reversely outputs all characters
In the previous article "PHP String Learning: Counting the Occurrences of Characters", we introduced the method of counting the occurrences of all characters or specified characters in a string. This time we introduce the method of reversing a string and outputting all the characters of the string in reverse order. You can refer to it if you need it.
The topic introduced to you today is to reversely output a string. There are many ways to achieve this operation. We can
traverse the string in reverse, and then Use the character concatenation "
.
" operator to splice the characters from back to front to splice a new string (reverse the string), and finally output it. [Recommended reading: "Teach you to use PHP operators to splice two strings together"]Split the string into an array, and then traverse Array, reversely splice the array elements into a new string, and finally output it.
First use the function to reverse the string directly, and then output the reversed string.
Let’s introduce them one by one, first look at the following example:
<?php function fz1($str){ $len=strlen($str); $res = ''; for($m=$len-1;$m>=0;$m--){ $res .= $str[$m]; } return $res; } echo fz1("abcdefg"); ?>
The output result is:
It can be seen that we use a for loop to reverse the order of the characters in the string "abcdefg", splice a new string "gfedcba", and then output the string.
Let’s take a look at another example:
<?php function fz1($str){ if (strlen($str) <= 1) return $str; $newstr = ''; /* * str_split(string,length) 函数把字符串分割到数组中: * string 必需。规定要分割的字符串。 * length 可选。规定每个数组元素的长度。默认是 1。 * */ $strarr = str_split($str,1); foreach ($strarr as $word) { $newstr = $word.$newstr; } return $newstr; } echo fz1("AbCdefg"); ?>
We use str_split($str,1)
to split the $str string into the $strarr array; then use foreach to traverse $strarr array, use the "$newstr = $word.$newstr
" statement in the loop body to reversely splice the array elements into a new string; finally output the new string.
Let’s take a look at the output results:
The above two examples are a bit complicated to reverse the string and output all the characters in reverse. In fact, PHP has a built-in function that can reverse a string in one step.
<?php $str="AbCdefg"; echo strrev($str); ?>
Output result:
Is it very simple? You only need one line of code strrev($str)
to reverse the string The order of the characters in the string is reversed.
Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial
Finally, I recommend reading a classic course "PHP String Processing (Jade Girl Heart Sutra Edition)", it's free~ come and learn !
The above is the detailed content of PHP string learning reversely outputs all characters. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software