How to get the first digits of a string in php

墨辰丷
Release: 2023-03-27 20:58:02
Original
8888 people have browsed it

This article mainly introduces the method of obtaining the first few characters of a string in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

In actual project applications, it is often encountered using php to obtain the first few digits of a string for comparison, assignment, etc. Today I will share with you how to use php substr to get the first few digits, last digits, and specified position of a string.

substr

(PHP 4, PHP 5)

substr — Returns a substring of a string

Description

string substr (string $string, int $start [, int $length])

Returns the substring of string string specified by the start and length parameters.

Parameters

string

Input string.

start

If start is a non-negative number, the returned string will start from the start position of string and start counting from 0. For example, in the string "abcdef", the character at position 0 is "a", the character at position 2 is "c", and so on.

If start is a negative number, the returned string will start from the start character forward from the end of string.

If the length of string is less than or equal to start, FALSE will be returned.

Example #1 Use negative start

<?php
$rest = substr(“abcdef”, -1); // 返回 “f”
$rest = substr(“abcdef”, -2); // 返回 “ef”
$rest = substr(“abcdef”, -3, 1); // 返回 “d”
?>
Copy after login


##length

If a positive length is provided, the returned string will contain up to length characters starting from start (depending on the length of the string).

If a negative length is provided, many characters at the end of the string will be missed (if start is a negative number, it will be counted from the end of the string). If start is not in this text, an empty string will be returned.

If length is provided with a value of 0, FALSE or NULL, an empty string will be returned.

If length is not provided, the returned substring will start from the start position until the end of the string.

Example #2 Use negative length

<?php
$rest = substr(“abcdef”, 0, -1); // 返回 “abcde”
$rest = substr(“abcdef”, 2, -1); // 返回 “cde”
$rest = substr(“abcdef”, 4, -4); // 返回 “”
$rest = substr(“abcdef”, -3, -1); // 返回 “de”
?>
Copy after login


Return value

Return the extracted substring, or FALSE on failure.

Change log version description

5.2.2 – 5.2.6 If the start parameter indicates the position of a negative truncation or beyond, false is returned. Other versions get the string from start.

Example


Example #3 basic usage of substr()

<?php
echo substr(‘abcdef&#39;, 1); // bcdef
echo substr(‘abcdef&#39;, 1, 3); // bcd
echo substr(‘abcdef&#39;, 0, 4); // abcd
echo substr(‘abcdef&#39;, 0, 8); // abcdef
echo substr(‘abcdef&#39;, -1, 1); // f

// 访问字符串中的单个字符
// 也可以使用中括号
$string = ‘abcdef&#39;;
echo $string[0]; // a
echo $string[3]; // d
echo $string[strlen($string)-1]; // f
?>
Copy after login


Example #4 substr() casting behavior

<?php
class apple {
public function __toString() {
return “green”;
}
}

echo “1) “.var_export(substr(“pear”, 0, 2), true).PHP_EOL;
echo “2) “.var_export(substr(54321, 0, 2), true).PHP_EOL;
echo “3) “.var_export(substr(new apple(), 0, 2), true).PHP_EOL;
echo “4) “.var_export(substr(true, 0, 1), true).PHP_EOL;
echo “5) “.var_export(substr(false, 0, 1), true).PHP_EOL;
echo “6) “.var_export(substr(“”, 0, 1), true).PHP_EOL;
echo “7) “.var_export(substr(1.2e3, 0, 4), true).PHP_EOL;
?>
Copy after login


The above routine will output:

1) 'pe'

2) '54'
3) 'gr'
4) '1'
5) false
6) false
7) '1200'

Error/Exception

Returns FALSE when there is an error.

<?php
var_dump(substr(‘a&#39;, 1)); // bool(false)
?>
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's study.


Related recommendations:

php uses eval to implement calculation formulas in the string format

PHP implementation for mixed Chinese and EnglishStringLength judgment and interception method

php str_getcsv implements characters StringMethod to parse into array

The above is the detailed content of How to get the first digits of a string in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!