Backend Development 5 min read

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:

<code>$string = 'Hello World';
$delete = 'l';
$newstring = str_replace($delete, '', $string);
echo $newstring;</code>

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:

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

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

Method 3: Using ereg_replace()

ereg_replace() performs regular‑expression replacement. Example:

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

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:

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

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

Programmingstring-manipulationpreg_replacestr_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

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