Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does php intercept the last few characters of a specified string

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how php intercepts the last few characters of a specified string. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

In php, you can use the substr () function to intercept the last few characters of a specified string. You only need to set the second parameter "$start" of the function to a negative number, and you can count forward the "$start" character from the end of the string to intercept all characters; syntax "substr ($str,$start)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

When dealing with strings, sometimes we need to intercept strings. In PHP, intercepting strings can be achieved through PHP's predefined function substr ().

The substr () function can intercept a certain length of characters from a specified position in a string. The intercepted characters can be called "substrings" or "substrings". The syntax format is as follows:

Substr ($string, $start [, $length])

The parameters are described as follows:

$string: the string to be intercepted, which contains at least one character

$start: the starting position of the intercepted string

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

If $start is negative, the string will count forward the first $start character at the end of $string, and the $start will start at-1. For example, in the string "abcdef", the character at-1 is "f", the character at-3 is "d", and so on

If the length of $string is less than $start, FALSE is returned.

$length: an optional parameter that indicates the length of the intercepted string.

If $length is positive, the string will intercept up to $length characters backwards from the $start position

If $length is negative, the $length characters at the end of $string will be omitted (from the end of the string if $start is negative)

If the value of $length is 0pm false or NULL, an empty string will be returned

If $length is not provided, the returned substring starts at the $start position and ends at the end of the string.

Example: intercept a few characters after a specified string

To intercept the last few characters of a specified string, simply set the second parameter "$start" of the substr () function to a negative number, and you can intercept all characters by counting forward the "$start" character at the end of the string.

Output:

This is the end of this article on "how to intercept a few characters after a specified string by php". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report