search
HomeBackend DevelopmentPHP ProblemHow to find the character of a string in php

How to find the character of a string in php

Feb 25, 2022 pm 06:55 PM
phpFind string

Methods to obtain characters: 1. Use the substr() function to intercept characters of a certain length from the specified position of the string. The syntax is "substr($str, character position, 1)"; 2. Use mb_substr () function, syntax "mb_substr($str, character position, 1, "character encoding")".

How to find the character of a string in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php finds the string Which character is

Method 1: Use the substr() function

substr() function can intercept a certain number from the specified position of the string Length of characters:

substr($string, $start , $length)

If you only want to get one character, you can set the $length parameter to 1.

Example:

<?php
$str = "Hello World";
echo substr($str, 0,1).&#39;<br>&#39;;
echo substr($str, 1,1).&#39;<br>&#39;;
echo substr($str, 2,1).&#39;<br>&#39;;
echo substr($str, 3,1).&#39;<br>&#39;;
echo substr($str, 4,1).&#39;<br>&#39;;
echo substr($str, 5,1).&#39;<br>&#39;;
echo substr($str, 6,1).&#39;<br>&#39;;
echo substr($str, 7,1).&#39;<br>&#39;;
echo substr($str, 8,1).&#39;<br>&#39;;
echo substr($str, 9,1).&#39;<br>&#39;;
echo substr($str, -1,1).&#39;<br>&#39;;
?>

How to find the character of a string in php

Method 2: Use mb_substr() function

mb_substr() The function can intercept a specified part from a string. Unlike the substr() function, the mb_substr() function is not only valid for English characters, but also for Chinese characters. Its syntax format is as follows:

mb_substr($str , $start [, $length = NULL [, $encoding = mb_internal_encoding()]])
  • $encoding: Optional parameter, indicating the character encoding of $str. If omitted, the internal character encoding is used.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$str = "欢迎来到PHP中文网";
echo mb_substr($str,0,1,"utf-8").&#39;<br>&#39;;
echo mb_substr($str,1,1,"utf-8").&#39;<br>&#39;;
echo mb_substr($str, 2,1,"utf-8").&#39;<br>&#39;;
echo mb_substr($str,3,1,"utf-8").&#39;<br>&#39;;
echo mb_substr($str,4,1,"utf-8").&#39;<br>&#39;;
echo mb_substr($str,5,1,"utf-8").&#39;<br>&#39;;
echo mb_substr($str,6,1,"utf-8").&#39;<br>&#39;;
echo mb_substr($str,7,1,"utf-8").&#39;<br>&#39;;
echo mb_substr($str,8,1,"utf-8").&#39;<br>&#39;;
echo mb_substr($str,9,1,"utf-8").&#39;<br>&#39;;
echo mb_substr($str,-1,1,"utf-8").&#39;<br>&#39;;
?>

How to find the character of a string in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to find the character of a string in 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 Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool