php string slice truncate any character in word or in a text

You want to remove the first or last n characters from a string, use substr()

The substr() function ist often when you need to slice off the required number of characters from the beginning or end of the string.

The substr() function gives you the
possibility to slice and dice strings into smaller strings.
It accepts three arguments, of which the last is optional:
the string to act on, the position to begin slicing at, and the
number of characters to return from its start position. A negative
value for the third argument tells PHP to remove characters from the
end of the string.

subtring function parameters:
This is the string to be sliced into smaller strings.
This is an integer value and it is the starting point of cutting the string.The counting begins
from zero.

  • length This is an optional parameter. If the length is a positive, meaning greater than zero.
  • The function will return the sliced string having the length you defined with regard to the start number you defined.
    If the length is negative php will count removing characters from the end of the string
 
<?php 
$string = "Boxing is said to be brutal sports, 
but many people love it";
$start= 0;
$length = 20;
//Get the substring of the string
echo substr($string, $start, $length);
?>
Output: Boxing is said to be

The following example works differently from the above one. It cuts a string using substr() function and begins it from the
end of the string.

<?php 
$string = "Boxing is said to be brutal sports, 
but many people love it";
$start= 0;
$length = -2;
//Get the substring of the string
echo substr($string, $start, $length); 
?>
Output: Boxing is said to be brutal sports, 
but many people love

Cutting off php String to maximum of Characters and use place holders
Limit the number of characters in a String to a max number using the php  substring()  function.

<?php
function cuttOffString($string, $maxChars=40, $placeHolder="...") {
// check string length
// truncate if necessary
if (strlen($string) > $maxChars) {
return trim(substr($string, 0, $maxChars)) . $placeHolder;
} else {
return $str;
}
}
//use the function 
$string = 'I may be writting very long text
but it will be cut off to a maximum number
of Characters';
echo cuttOffString($string);
?>
output :I may be writting very long text...

Remove characters from a very long word in text to avoid layout overflow or break.

cut the unwanted characters of a word in a text using substr() function.
This can be very useful, fore instance when you have a very long word in text and you want
want to limit the number of these characters. Some times it can happen that users of a system, especially in
comment sections, write very long word, like kkkkkkkk which can cause overflow of your layout.

<?php
$string ="ell checking module available for PHP, but if it’s not already installed Figure 3-8 again 
shows a kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk’ A Tale of Two Cities";
$myArray = explode(' ', $string);
foreach ($myArray as $arr):
if (strlen($arr) > 18){
echo substr($arr, 0, 18);
}else{
echo "$arr &lt;br/&gt;";
}
endforeach;
?>
output :ell
checking
module
available
for
PHP,
but
if
it’s
not
already
installed
Figure
3-8
again
shows
a
kkkkkkkkkkkkkkkkkkkkkkA
Tale
of
Two
Cities