Trimming String using PHP

by Vincy. Last modified on June 2nd, 2021.

String Trimming is the process of clearing the unwanted whitespace present in a string. This process will clear the whitespace that occurs around the string, but will not clear the unwanted space in the middle of the string.

Trimming will be useful when we process the user data which may contain unwanted whitespace by mistake.

PHP contains built-in functions to do trimming on a string input. Those are varied based on the position on which the trimming is going to be implemented.

  • trim() – To strip the whitespace that occurs before and after the string to be trimmed.
  • ltrim() – To stripe the whitespace that occurs before the string.
  • rtime() – To strip the whitespace at the end of the string.

View Demo

PHP trim functions contain two parameters. One is the actual string which is to be trimmed and it is mandatory. The other parameter is the character mask to specify the list of characters to be stripped from the original string while trimming.

The trimming functions stripe whitespace by default. By providing the character mask the default option is overridden.

The following code shows how to use the character mask parameter in the PHP trimming functions. In this code, I have specified the escape sequences tab(\t), newline(\n) and the carriage return(\r) for the character mask parameters that are to be stripped from the original string while trimming.

$strWord = " Welcome\t to\t PHPPOT\n ";
//This line will strip the unwanted white spaces, tabs, 
//and new line breaks from the given string.
$trimmedWord = trim($strWord, " \r\n\t");

PHP Example for Applying Various Trimming

This code is used to show HTML form to get the user input string and select the trimming type to be applied to the input string. On submitting the input string to the PHP, I process this input string by applying the selected trimming type.

<div id="trim-form-container">
    <form name="frmTrim" action="" method="post">
        <div class="demoInput">
            <textarea type="text" name="strUserInput" placeholder="Enter your data"></textarea>
        <div class="demoInput">
            <select name="trimType">
                <option value="trim">trim()</option>
                <option value="ltrim">ltrim()</option>
                <option value="rtrim">rtrim()</option>
        <div class="demoInput">
            <input type="submit" name="apply_trim" value="Apply Trim" />
    if($_POST["apply_trim"]) {
        switch($_POST["trimType"]) {
            case "trim":
                $trimmedString = trim($_POST["strUserInput"]);
            case "ltrim":
                $trimmedString = ltrim($_POST["strUserInput"]);
            case "rtrim":
                $trimmedString = rtrim($_POST["strUserInput"]);
<div class="output">
    <div class="output-row"><label>Trimming Type Applied:</label><span class="output-value"><?php echo $_POST["trimType"]; ?></span></div>
    <div class="output-row"><label>Before Trimming:</label><span class="output-value highlight-space"><?php echo $_POST["strUserInput"]; ?></span></div>
    <div class="output-row"><label>After Trimming:</label><span class="output-value highlight-space"><?php echo $trimmedString; ?></span></div>

PHP String Trimming Output

This screenshot shows the output for the PHP string trimming by showing the original string, processed string and the selected trimming type.


View DemoDownload

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top