All articles

Work with PHP values and data

Trim, Search, and Replace Text in a PHP String

Clean a PHP string, check its byte length, find a term without missing position 0, and replace every occurrence.

Editorial illustration for Pad or Truncate an ASCII Label to Seven Positions in PHP

To clean an input string and change a term, trim whitespace from its ends, inspect the result, search for the term, then replace it. The important search choice is whether you need the position of a match or only a yes-or-no answer. This example starts with spaces around PHP PHP tips and changes both occurrences of PHP to php.

Clean and inspect one string

The following standalone example includes the opening PHP tag. It prints checks at each step so that a match at position zero cannot be confused with a missing term. Because it uses str_contains(), the example requires PHP 8.0 or later.

<?php
$text = '  PHP PHP tips  ';
$clean = trim($text);

echo 'clean: [' . $clean . "]\n";
echo 'length: ' . strlen($clean) . "\n";

$position = strpos($clean, 'PHP');
echo 'first position: ' . $position . "\n";

if ($position !== false) {
    echo "PHP found: yes\n";
} else {
    echo "PHP found: no\n";
}

$absentPosition = strpos($clean, 'Ruby');
if ($absentPosition === false) {
    echo "Ruby position: false\n";
} else {
    echo 'Ruby position: ' . $absentPosition . "\n";
}

if (str_contains($clean, 'PHP')) { // PHP 8.0+
    echo "contains PHP: true\n";
} else {
    echo "contains PHP: false\n";
}

$changed = str_replace('PHP', 'php', $clean);
echo 'changed: [' . $changed . "]\n";

For these inputs, the expected lines are:

clean: [PHP PHP tips]
length: 12
first position: 0
PHP found: yes
Ruby position: false
contains PHP: true
changed: [php php tips]

The brackets are printed markers, not part of either string. They let you see where the cleaned and changed text begins and ends.

Trim the ends, then measure

trim($text) removes the surrounding whitespace in this example. It does not remove the spaces within PHP PHP tips: one separates the two occurrences of PHP, and another separates the second occurrence from tips. The PHP string-function reference describes trim() as stripping whitespace or other characters from the beginning and end of a string.

After trimming, $clean is PHP PHP tips. Its strlen() result is 12: three bytes for the first PHP, one space, three bytes for the second PHP, one more space, and four bytes for tips. Every character used in this example is ASCII, so that byte count is also the count a reader gets by counting the visible letters and spaces. Do not generalize that agreement to multibyte text. PHP strings are byte sequences, and core functions such as strlen() operate on bytes. When a multibyte character count matters instead, the multibyte-function discussion identifies mb_strlen() in the mbstring extension as an alternative.

Choose a position or a yes-or-no search

strpos($clean, 'PHP') finds the first occurrence and reports its zero-based position. Here the first PHP begins at the very start of $clean, so the position is 0. That number is useful if the next step needs to know where the term begins. It is not a failure indicator.

An absent term gives a different result: strpos($clean, 'Ruby') returns false. Use !== false to test whether a search succeeded, or === false to identify a miss. A loose comparison would be a poor fit here because it can treat a valid position of 0 as false. The separate Ruby check makes the distinction visible instead of relying on a printed number to tell the whole story.

If the only question is whether the term occurs anywhere, str_contains($clean, 'PHP') gives a boolean without requiring a position check. Use it when the position adds nothing to your decision, but keep its PHP 8.0+ requirement in mind. The position-bearing strpos() result remains the useful choice when you need the first match's location.

Replace the term and inspect the result

str_replace('PHP', 'php', $clean) replaces all occurrences of the search string with the replacement string. Assigning its result to $changed makes it straightforward to print the text after the change. The expected changed: [php php tips] line shows that both PHP terms changed, not just the first one that strpos() located.

Notice the separation between the checks: the search asks about $clean, while the final line displays $changed. Finding the first position does not restrict str_replace() to that occurrence. If your task is simply to change every matching term, the replacement call does that job; use the earlier search when its answer is needed for a separate decision.

← All articles