How to Cut a String After a Certain Number of Characters In PHP?

Solution of How to Cut a String After a Certain Number of Characters.

Problem:

You have a long string. You want to extract first 20 characters from it.

Solution:

Use substr() function.

Example:

<?php
$text = “How to Cut a String After a Certain Number of Characters In PHP?”;
$first_fifty = strlen($text) > 20 ? substr($text, 0, 20) : $text;
echo $first_fifty;
?>

Output:
How to Cut a String

Explanation:

Line 3 If the text length is more than 20 characters long(strlen($text) > 20), the first 20 characters are extracted (substr($text, 0, 20)). Here, 0 means the extracting starts from the beginning of the string. On the other hand, if the text length is less than 20, the function returns the text without truncating($text which is the variable after : of the ternary operator)
Line 4 The 20 characters are printed.

substr() function at a glance

(PHP4, PHP5)
Usages:
substr() function returns part of a string.

Syntax:
substr(string, start, length)

Parameters:

Parameter What it does
string Required Specifies the supplied string. It must be one or more character long.
start Required Specifies the position in the string from where the extraction will begin.

  1. A positive start number indicates the start’th position from the beginning of the string. Here, the first position is zero.
  2. A negative start number indicates the start’th position from the end of the string.
  3. If the length of the string is less than or equal to start, false will be returned.
length Optional Specifies the length of the string to be returned.

  1. A positive length number indicates that the returned string will contain length characters beginning from start.
  2. A negative length number indicates the number of characters deleted from the end of the string
  3. If length is 0, an false or null will be returned.
  4. If length is not specified, the returned string will begin from the start and ends at the end of the string.

Variations 1: When the start is a negative number.

Example :

<?php
echo substr(“abcdefghijk”, -3);
?>

Output:
ijk

Explanation:

Line 2 As the second parameter is negative -3, the returned string starts from the 3rd character(i) from the end of the string. And, as the third parameter is absent, the function will return all the characters to the end.

Variations 2: When the length of the string is less than the position number of the string where the extraction will begin (second parameter).

Example :

<?php
$return_val = substr("abcdefghijk", 12);
if($return_val == false) echo "False returned.";
?>

Output:
False  returned.

Explanation:

Line 2 The length of the string is 11 and the starting position number of the string to be extracted is 12, so, the function returns false

Variations 3: When the start and length are both negative numbers.

Example :

<?php
echo substr(“abcdefghijk”, -3, -1);
?>

Output:
ij

Explanation:

Line 2 As the second parameter is negative -3, the returned string starts from the 3rd character(i) from the end of the string. And, as the third parameter is negative -1, so the last character(k) will be deleted. So the returned characters are ij.

Variations 4: When the length of the string to be returned is 0.

Example :

<?php
$return_val = substr("abcdefghijk", 3, 0);
if($return_val == false)
echo "False returned.";
?>

Output:
False returned.

Explanation:

Line 2 As the third parameter is 0, the function returns false.

Variations 5: When the length of the returned string is not specified.

Example :

<?php
echo substr("abcdefghijk", 3);
?>

Output:
defghijk

Explanation:

Line 2 As the third parameter is not specified, the function returns all the characters from the 3rd(position starts from 0) to the end.


Pin It

↑ Back to Top