Home Backend Development PHP Tutorial How to get the first digits of a string in php

How to get the first digits of a string in php

May 25, 2018 am 09:40 AM
php string method

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!

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles