Example of a PHP intercept string

Source: Internet
Author: User

An example

Example 1

  
"Http://www.w3.org/TR/html4/loose.dtd" >


<metahttp-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Apply the substr () function to intercept characters of a specified length in a string </title>

<body>

echo substr ("www.goodby.com", 0);//intercept from NO. 0 character
echo "<br>";
echo substr ("www.goodby.com", 4,10);//10 characters in a row starting from the 4th character
echo "<br>";
echo substr ("www.goodby.com", -4,4)//intercept 4 characters from the penultimate 4th
echo "<br>";
echo substr ("www.goodby.com", 0,-4), intercept from the first character, intercept to the penultimate 4th character
?>
</body>

Results

Www.goodby.com

Goodby.com

. com

Www.goodby

Example 2

  
"Http://www.w3.org/TR/html4/loose.dtd" >


<metahttp-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> Apply the substr () function to intercept part of a string of extra long text, and the remainder is replaced by "..." </title>


<body>
<?php
$str = "To further enrich our lives, the company decided to organize the" Winter Love film Cup "photography competition,
The entries are required to be filmed in winter, and are designed to showcase the beautiful scenery of the northern part of the country. ";
if (strlen ($STR) >70) {
Echo substr ($str, 0,70). " ...";
}
else{
Echo $str;
}
?>
</body>

Results

To further enrich our lives, the company decided to organize the "Winter Love Film Cup" photography contest, this tournament ...

Two substr function description

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

Returns the substring specified by the start and length arguments in string strings.

Parameters

String

The input string. Must have at least one character.

Start

If Start is Non-negative, the returned string starts at the start position of string and is calculated starting at 0. For example, in the string "ABCdef", the character at position 0 is "a", and the string at position 2 is "C" and so on.

If start is a negative number, the returned string starts with the first character starting at the end of the string.

Returns False if the length of a string is less than start.

Length

If the length of a positive number is provided, the returned string will start at the beginning with a maximum of length characters (depending on the length of the string).

If the length of a negative number is provided, the length character at the end of the string will be omitted (if start is a negative number from the tail of the string). If start is not in this text, FALSE is returned.

If length is provided with a value of 0,false or null, an empty string is returned.

If length is not supplied, the returned substring starts at the start position until the end of the string.

return value

Returns the extracted substring or FALSE when it fails.

Source: http://cakin24.iteye.com/blog/2348829

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.