Home Backend Development PHP Tutorial Let's talk about the correct usage of substr and substring in PHP and the introduction of related parameters

Let's talk about the correct usage of substr and substring in PHP and the introduction of related parameters

Jul 29, 2016 am 09:10 AM
echo length start substr substring

Everyone knows that there are functions substr and substring for string interception in js. What about php? PHP does not have a directly available substring function, but it does have a substr function.
If you don’t believe it, you can test it yourself. A correct piece of code is given below.

<&#63;
$a="me";
echo(substr($a,,));//输出me
&#63;>
下面又给出一段错误的代码
<&#63;
$a="me";
echo(subString($a,,));
&#63;>
Copy after login

substr() function returns a part of a string.

substr(string,start,length)

string: The string to be intercepted

start:

Positive number - starts at the specified position of the string
Negative number - starts at the specified position from the end of the string
0 - Start at the first character in the string

length:

Optional. Specifies the length of the string to be returned. The default is until the end of the string.
Positive number - returns from the position of the start parameter
Negative number - returns from the end of the string

Detailed explanation of the usage of PHP substr()

Definition and usage

substr() function returns a part of the string. Using the substr() function to intercept Chinese may cause garbled characters. It is recommended to use the mb_substr() function to intercept Chinese.

Syntax

substr(string,start,length)

Parameters Description
string Required. Specifies a part of the string to be returned.
start

Required. Specifies where in the string to begin.

  • Positive number - starts at the specified position in the string
  • Negative number - starts at the specified position from the end of the string
  • 0 - starts at the first character in the string
length

Optional. Specifies the length of the string to be returned. The default is until the end of the string.

  • Positive number - returned from the position of the start parameter
  • Negative number - returned from the end of the string

Tips and Notes

Note: If start is a negative number and length is less than or equal to start, then length is 0.

Example

<&#63;php
$str = 'hello world!';
echo substr($str, 4); // o world! 左起第4开始向右截取到末尾
echo substr($str, 4, 5); // o wor 左起第4开始向右取5位
echo substr($str, 4, -1); // o world 左起第4与右起第1之间的字符
echo substr($str, -8, 4); // o wo 右起第8开始向右截取4位
echo substr($str, -8,-2); // o worl 右起第8与右起第2之间的字符
&#63;>
Copy after login

The above has introduced the correct usage of substr and substring in PHP and the introduction of related parameters, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

What should I do if docker start cannot start? What should I do if docker start cannot start? Oct 21, 2022 pm 03:43 PM

Solution to docker start failure: 1. Check the running status, and then release the occupied memory through the "echo 3 &gt; /proc/sys/vm/drop_caches" command; 2. Use "$netstat -nltp|grep .. ." command to check whether the port has been occupied. If it is found to be occupied after going online, change it to an available port and restart.

How to use the substring() function of the StringBuilder class in Java to intercept the substring of a string How to use the substring() function of the StringBuilder class in Java to intercept the substring of a string Jul 24, 2023 pm 12:13 PM

How does Java use the substring() function of the StringBuilder class to intercept a substring of a string? In Java, we often need to process string operations. Java's StringBuilder class provides a series of methods to facilitate us to operate strings. Among them, the substring() function can be used to intercept substrings of strings. The substring() function has two overloaded forms, namely substring(intstar

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

In today's era of rapid technological development, programming languages ​​are springing up like mushrooms after a rain. One of the languages ​​that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

Use java's String.length() function to get the length of a string Use java's String.length() function to get the length of a string Jul 25, 2023 am 09:09 AM

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

Go language development essentials: 5 popular framework recommendations Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

PHP returns the ASCII value of the first character of the string PHP returns the ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values ​​are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

See all articles