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")".
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).'<br>'; echo substr($str, 1,1).'<br>'; echo substr($str, 2,1).'<br>'; echo substr($str, 3,1).'<br>'; echo substr($str, 4,1).'<br>'; echo substr($str, 5,1).'<br>'; echo substr($str, 6,1).'<br>'; echo substr($str, 7,1).'<br>'; echo substr($str, 8,1).'<br>'; echo substr($str, 9,1).'<br>'; echo substr($str, -1,1).'<br>'; ?>
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").'<br>'; echo mb_substr($str,1,1,"utf-8").'<br>'; echo mb_substr($str, 2,1,"utf-8").'<br>'; echo mb_substr($str,3,1,"utf-8").'<br>'; echo mb_substr($str,4,1,"utf-8").'<br>'; echo mb_substr($str,5,1,"utf-8").'<br>'; echo mb_substr($str,6,1,"utf-8").'<br>'; echo mb_substr($str,7,1,"utf-8").'<br>'; echo mb_substr($str,8,1,"utf-8").'<br>'; echo mb_substr($str,9,1,"utf-8").'<br>'; echo mb_substr($str,-1,1,"utf-8").'<br>'; ?>
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!

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

Atom editor mac version download
The most popular open source editor

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

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
