Monday, 13 August 2018

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

Problem:

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

Solution:

Use substr() function.
Example:
1
2
3
4
5
<?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 3If 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 4The 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:
ParameterWhat it does
stringRequiredSpecifies the supplied string. It must be one or more character long.
startRequiredSpecifies the position in the string from where the extraction will begin.
  1. A positive startnumber 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.
lengthOptionalSpecifies the length of the string to be returned.
  1. A positive lengthnumber indicates that the returned string will contain length characters beginning from start.
  2. A negative lengthnumber 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 startand ends at the end of the string.
Variations 1: When the start is a negative number.
Example :
1
2
3
<?php
echo substr(“abcdefghijk”, -3);
?>
Output: 
ijk
Explanation:
Line 2As 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 :
1
2
3
4
<?php
$return_val = substr("abcdefghijk", 12);
if($return_val == false) echo "False returned.";
?>
Output:False  returned.
Explanation:
Line 2The 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 :
1
2
3
<?php
echo substr(“abcdefghijk”, -3, -1);
?>
Output: 
ij
Explanation:
Line 2As 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 :
1
2
3
4
5
<?php
$return_val = substr("abcdefghijk", 3, 0);
if($return_val == false)
echo "False returned.";
?>
Output: 
False returned.
Explanation:
Line 2As the third parameter is 0, the function returns false.
Variations 5: When the length of the returned string is not specified.
Example :
1
2
3
<?php
echo substr("abcdefghijk", 3);
?>
Output: 
defghijk
Explanation:
Line 2As the third parameter is not specified, the function returns all the characters from the 3rd(position starts from 0) to the end.

0 comments:

Post a Comment