AppDividend
Latest Code Tutorials

PHP chop() Function Example | PHP String chop() Method

0

PHP chop() Function Example | PHP String chop() Method is today’s topic. The chop() function removes whitespaces or other predefined characters from the right end of a string. The chop() in PHP String is used to remove the white spaces or any other specified characters from the end of the string. A return type of the chop() function is a string. It returns a string after removing the specified characters from an end. The chop() function is an alias of rtrim() function.

PHP chop() Function

The syntax of PHP String chop() Function is following.

string chop($string, $character)

The $string argument is which is needed to be checked.

The $character argument specifies the character which is needed to be removed from a given string. If the parameter is not specified, then NULL, tab, newline, vertical tab, carriage return, and ordinary white space are removed automatically.

See the following characters are removed if a charlist parameter is empty:

  1. “\0” – NULL
  2. “\t” – tab
  3. “\n” – new line
  4. “\x0B” – vertical tab
  5. “\r” – carriage return
  6. ” ” – ordinary white space

See the code example.

<?php

$site = 'AppDividend';
echo chop($site, 'end');

See the output.

➜  pro php app.php
AppDivi                                                                         
➜  pro

#Pass no parameters

In the below code, since no character parameter is mentioned, it automatically the newlines will be removed from a given string.

<?php

$statement = "Hello Millie! \n nice to meet you \n \n"; 
echo chop($statement);

See the output.

➜  pro php app.php
Hello Millie!
 nice to meet you                                                               
➜  pro

#How to remove the last character in string in PHP

We can use the chop() function and rtrim() function to remove the last character of the string.

<?php

$entrepreneur = "MillieBobbyBrown"; 
echo chop($entrepreneur, 'n');

See the output.

➜  pro php app.php
MillieBobbyBrow                                                                 
➜  pro

Okay, let’s use the rtrim() function.

#rtrim() in PHP

See the code.

<?php

$entrepreneur = "MillieBobbyBrown"; 
echo rtrim($entrepreneur, 'n');

See the output.

➜  pro php app.php
MillieBobbyBrow                                                                 
➜  pro

Finally, PHP chop() Function Example | PHP String chop() Method is over.

Recommended Posts

  1. PHP money_format() Function Example Tutorial
  2. PHP number_format() Function Example | number_format() In PHP
  3. PHP unset Example | unset() Function In PHP Tutorial
  4. PHP strstr() Function Example | strstr() Method In PHP
  5. PHP Password Hashing Functions API Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.