Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

PHP String chop() Function

Posted in PHP Tutorial
Updated on Nov 22, 2024
By Mari Selvan
👁ī¸ 51 - Views
âŗ 4 mins
đŸ’Ŧ 1 Comment
PHP String chop() Function

Photo Credit to CodeToFun

🙋 Introduction

In PHP, manipulating strings is a common task, and various functions are available for string operations.

The chop() function is one such function that removes whitespace or other specified characters from the end of a string. It is essentially an alias for the rtrim() function.

In this tutorial, we'll explore the usage and functionality of the chop() function in PHP.

💡 Syntax

The syntax for the chop() function is as follows:

Syntax
Copied
Copy To Clipboard
string chop(string $string, string $characters = " \t\n\r\0\x0B" )
  • $string: The input string to be trimmed.
  • $characters (optional): A list of characters to be removed. If not specified, it removes whitespaces.

📄 Example

Let's delve into an example to illustrate how the chop() function works.

chop.php
Copied
Copy To Clipboard
<?php
// Example 1
$text1 = "Hello, World!   ";
$trimmed1 = chop($text1);

echo "Example 1: '$trimmed1'\n"; // Output: 'Hello, World!'
// Example 2
$text2 = "   PHP is awesome   \t";
$trimmed2 = chop($text2, " \t");

echo "Example 2: '$trimmed2'\n"; // Output: '   PHP is awesome'

?>

đŸ’ģ Output

Output
Example 1: 'Hello, World!'
Example 2: '   PHP is awesome'

🧠 How the Program Works

In Example 1, the chop() function removes trailing whitespaces from the string "Hello, World!".

In Example 2, it removes leading and trailing whitespaces and tabs from the string "PHP is awesome".

↩ī¸ Return Value

The chop() function returns the trimmed string.

📚 Common Use Cases

The chop() function is useful when you want to remove trailing whitespaces or specific characters from the end of a string. It's commonly used to clean up user input or prepare data for further processing.

📝 Notes

  • chop() is essentially an alias for the rtrim() function. They behave identically.
  • The default list of characters to remove includes space, tab, newline, carriage return, null byte, and vertical tab.

đŸŽĸ Optimization

The chop() function is optimized for string trimming. If you have specific character removal needs, consider using rtrim() with custom character lists for better control.

🎉 Conclusion

The chop() function in PHP is a convenient tool for removing trailing whitespaces or specified characters from the end of a string. It provides a clean and readable way to sanitize and manipulate strings in your PHP applications.

Feel free to experiment with different strings and explore the behavior of the chop() function in various scenarios. Happy coding!

👨‍đŸ’ģ Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
9 months ago

If you have any doubts regarding this article (PHP String chop() Function), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy