Home >Backend Development >PHP Problem >How to find the character of a string in php

How to find the character of a string in php

青灯夜游
青灯夜游Original
2022-02-25 18:55:273886browse

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