PHP String Replacement
Simple text changes do not always require a regular expression. Use a literal operation when the requirement is literal and reserve patterns for actual pattern rules.
How it works
trim removes the configured set of boundary characters, not arbitrary substrings. str_replace replaces literal occurrences. explode and implode split and join around a literal separator. Each operation produces a result that must be stored or used; it does not rewrite an unrelated variable automatically. A separator may also appear inside data, so CSV requires a CSV parser rather than an unconditional explode.
Run it locally
Run this complete source locally with PHP 8.2 or newer. Save it using the filename shown and run the command below. PHP is not currently available in Bhylu’s browser playground. Apache and MySQL are not needed for an ordinary CLI example.
php -l string-transform.php
php string-transform.phpDownload this source as a local PHP lab file. Keep lab files separate from the Bhylu project and production data.
Example
<?php
declare(strict_types=1);
$raw = " Spade|Drill|Rake ";
$items = explode("|", trim($raw));
$label = implode(", ", $items);
echo str_replace("Drill", "Hand drill", $label), "\n";
Expected result
Spade, Hand drill, RakeWhitespace is removed only at the outer boundary. Splitting then produces three literal names, and joining builds a display label. The final replacement creates a modified result without changing items. This fixture deliberately excludes the delimiter from names; relaxing that rule would require an escaping or structured-format contract.
Check the official PHP manual for detailed version and API contracts.