Home > Backend Development > PHP Tutorial > #Learn a new php function every day (1) substr()

#Learn a new php function every day (1) substr()

WBOY
Release: 2016-08-08 09:25:15
Original
921 people have browsed it

Recently I found that my efficiency in writing code is too low. After searching for the reason, I found that most of the time was spent looking up the manual to find the usage of the function, but after using it at the time, I immediately forgot about it. The second time I encountered this function, I had to start over. A lot of time wasted. So I decided to write a collection of summaries, trying to record the most frequently encountered functions every day.

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

This function is used to intercept the specified string and is very powerful.

param $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 negative, the returned string will start characters start from the end of string.
If the length of string is less than or equal to start, FALSE is returned.

param $length

If a positive length is provided, the returned string will contain up to length characters starting from start (depending on the length of string).
If a negative length is provided, many characters from the end of the string (or from the end of the string if start is negative) will be missed. 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 is returned.
If length is not provided, the substring returned will start at the start position and continue until the end of the string.

Return Value

Returns the extracted substring, or FALSE on failure.

Example

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

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

The above introduces #Learn a new PHP function every day (1) substr(), including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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