search
HomeBackend DevelopmentPHP ProblemHow to find the first non-repeating character in a given string via PHP

A new week has begun again~ Everyone should know that the operation of strings in PHP is the most critical and basic part of programming. In the following articles, we will successively introduce to you the basic operations of strings. , I believe there are always skills you can master~

Then the topic of this article is "Write a PHP program to find the first non-repeating character in a given string".

For non-repeating characters, there should be no need to explain too much. For example, in the string "adicvdda", the first non-repeating character visible to the naked eye is i, because the characters a and d are both repeated.

Now we will introduce to you how to implement this operation through PHP.

The PHP code is as follows:

<?php
function find_non_repeat($word) {
    $chr = null;
    for ($i = 0; $i <= strlen($word); $i++) {
        if (substr_count($word, substr($word, $i, 1)) == 1) {
            return substr($word, $i, 1);
        }
    }
}

echo find_non_repeat("Green")."<br>";
echo find_non_repeat("abcdea")."<br>";

The output result is:

G
b

That is to say, the first non-character string in the given string "Green" The repeating character is G, and the first non-repeating character in "abcdea" is "b".

In the above code, we mainly traverse each character in the string through a for loop, and then compare it.

PHP strlen()The function is used to return the length of the string. The return value is the length of the string if successful, or 0 if the string is empty.

PHP substr_count() The function is used to count the number of times a substring appears in a string. The return value is the number of times the substring appears in the string.

The syntax is "substr_count(string,substring,start,length)";

parameters respectively represent:

string is required, Specifies the string to be checked.

substring is required and specifies the string to be searched.

start is optional and specifies where to start searching in the string.

length is optional and specifies the length of the search.

PHP substr() The function is used to return a part of the string. The return value is the extracted part of the returned string. If it fails, it returns FALSE, or returns an empty string. .

Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!

The above is the detailed content of How to find the first non-repeating character in a given string via PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!