chop() function in PHP || PHP Chop Function

chop() function in php

Last Updated by Pooja Chikara 27-Apr-20

0 336


PHP chop() function is used to remove whitespaces, some predefined characters, or user-defined characters or words from the right end of a PHP string. It is an inbuilt function of PHP. The return type of this function is a string.

The predefined characters are:

  • Null: ["\0"]
  • Tab: ["\t"]
  • New line; ["\n"]
  • Vertical tab: ["\x0B"]
  • Carriage return: ["\r"]
  • Simple white spaces: [" "]

Syntax:

chop($string,$character_list);

here,

  • $string is a PHP string.
  • $character_list is a list of words or characters which we want to remove from given string.

Example 1:

<?php
$string="Ram's son goes to his school in the morning";
$new_string=chop($string,"morning");
echo "Old string is : $string <br>";
echo "New String is : $new_string";
?>

Output:

chop() function in php


Example 2:

<?php
$string="Ram's son goes to his school in the morning.\n";
$new_string=chop($string); // it will remove \n from the end of $string
echo "Old string is : $string";
echo "New String is :$new_string";
?>

Output:

chop() function in php


Share:


Leave a Reply


Comments
    Waiting for your comments