How to Delete a Substring in PHP Using Various Functions

This article explains four PHP techniques—str_replace, substr_replace, ereg_replace, and preg_replace—for removing specific characters or patterns from a string, providing code examples and describing the resulting output for each method.

php Courses
php Courses
php Courses
How to Delete a Substring in PHP Using Various Functions

PHP is a popular dynamic programming language widely used for web development, and developers often need to remove a specific part of a string. This article introduces four ways to delete substrings in PHP.

Method 1: Using str_replace()

The str_replace() function replaces occurrences of a substring with another string. By replacing the target with an empty string, you can delete characters. Example:

$string = 'Hello World';
$delete = 'l';
$newstring = str_replace($delete, '', $string);
echo $newstring;

This code removes all "l" characters from "Hello World", resulting in "Heo Word".

Method 2: Using substr_replace()

The substr_replace() function deletes a portion of a string based on a start position and length. Example:

$string = 'Hello World';
$start = 2;
$delete = 5;
$newstring = substr_replace($string, '', $start, $delete);
echo $newstring;

Here, five characters starting from position 2 are removed, producing "He World".

Method 3: Using ereg_replace()

ereg_replace()

performs regular‑expression replacement. Example:

$string = 'Hello World';
$pattern = '/W/';
$replacement = '';
$newstring = ereg_replace($pattern, $replacement, $string);
echo $newstring;

This removes the character "W" from "Hello World", yielding "Heollo orld".

Method 4: Using preg_replace()

preg_replace()

also uses regular expressions but offers more powerful pattern matching. Example:

$string = 'Hello World';
$pattern = '/[aeiou]/';
$replacement = '';
$newstring = preg_replace($pattern, $replacement, $string);
echo $newstring;

The code deletes all vowel letters from "Hello World", resulting in "Hll Wrld".

In summary, four PHP functions— str_replace, substr_replace, ereg_replace, and preg_replace —can be used to delete specific parts of a string, and the choice depends on the required flexibility and context.

PHP Learning Recommendations

Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Master Course

Workerman+TP6 Real‑time Chat System – Limited Time Offer

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

preg_replacestring-manipulationstr_replacesubstr_replace
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.