PHP chop Function
PHP

PHP chop Function

Editorial Team
Editorial Team

Follow us on Discord or Telegram to get access to free tutorials and updates.

The PHP chop function is used to remove white-space or other characters from the right end of a string. This is an alias of rtrim() function and is case-sensitive.

Syntax

chop( string, chars_to_remove);

  • Case sensitive.
  • chars_to_remove is optional.
  • string is a required parameter.

If no characters to remove is specified (the second parameter is not specified), the following characters will be removed from the end of the string.

  • ” ” – White-space.
  • “\t” – A tab.
  • “\n” – A new line.
  • “\r” – a carriage return.
  • “\0” – the NULL-byte.
  • “\x0B” – a vertical tab.

Example 1

<?php
    $data = "PHP Chop will remove pre defined chars if second parameter is not specified        \n";
    echo chop($data);
?>

Output

PHP Chop will remove pre defined chars if second parameter is not specified

Example 2

<?php
    $data = "PHP Chop";
    echo chop($data, "Chop");
    echo "<br>"; //New line
    echo chop($data, "chop"); //C will not be removed
?>

Output

PHP
PHP C

Learn more about other PHP string handling functions.