Trimming String using PHP

by Vincy. Last modified on August 1st, 2022.

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
$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 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.

<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>
		<div class="demoInput">
			<select name="trimType">
				<option value="trim">trim()</option>
				<option value="ltrim">ltrim()</option>
				<option value="rtrim">rtrim()</option>
			</select>
		</div>
		<div class="demoInput">
			<input type="submit" name="apply_trim" value="Apply Trim" />
		</div>
	</form>
</div>
<?php
if ($_POST["apply_trim"]) {
    switch ($_POST["trimType"]) {
        case "trim":
            $trimmedString = trim($_POST["strUserInput"]);
            break;
        case "ltrim":
            $trimmedString = ltrim($_POST["strUserInput"]);
            break;
        case "rtrim":
            $trimmedString = rtrim($_POST["strUserInput"]);
            break;
    }
    ?>
<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>
</div>
<?php
}
?>

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.

php-string-trimming-output

View DemoDownload

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

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

↑ Back to Top

Share this page