Home  >  Article  >  Backend Development  >  How to get the first digits of a string in php

How to get the first digits of a string in php

藏色散人
藏色散人Original
2021-09-22 10:22:334593browse

In PHP, you can use the substr function to get the first few digits of a string. The implementation method is such as "substr("abcdef", 0, -1);", and its return value is the number in the specified string. The first few.

How to get the first digits of a string in php

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

How to get the first digits of a string in php?

Instance of php getting the first few digits of a string (substr returns the substring usage of the string)

In actual project applications, it is often encountered using php to get the first few digits of a string. Bits are used 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 a negative number start

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

length

If a positive length is provided, the string returned Will contain up to length characters starting from start (depending on the length of 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 a 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”
?>

Return value

Returns the extracted substring, or returns 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
?>

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;
?>

The above routine will output:

##1) 'pe'

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

Error/Exception

Error Return FALSE.

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

Recommended learning: "

PHP Video Tutorial"

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!

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