Skip to main content
PHP Blog

Back to all posts

How to Replace A Symbol In A Text String In PHP?

Published on
4 min read
How to Replace A Symbol In A Text String In PHP? image

Best PHP String Manipulation Tools to Buy in October 2025

1 PHP in easy steps: Updated for PHP 8

PHP in easy steps: Updated for PHP 8

BUY & SAVE
$13.86 $16.99
Save 18%
PHP in easy steps: Updated for PHP 8
2 Programming PHP: Creating Dynamic Web Pages

Programming PHP: Creating Dynamic Web Pages

BUY & SAVE
$34.67
Programming PHP: Creating Dynamic Web Pages
3 PHP Beginner's Practical Guide

PHP Beginner's Practical Guide

  • IN-DEPTH PHP & MYSQL WITH PRACTICAL, EXAMPLE-DRIVEN LEARNING.
  • ALIGNS PERFECTLY WITH B.E./B.TECH SYLLABUS FOR EFFECTIVE TRAINING.
  • SIMPLIFIED LANGUAGE AND CLASSROOM STYLE FOR EASY COMPREHENSION.
BUY & SAVE
$14.95
PHP Beginner's Practical Guide
4 Programming PHP: Creating Dynamic Web Pages

Programming PHP: Creating Dynamic Web Pages

BUY & SAVE
$16.29 $39.99
Save 59%
Programming PHP: Creating Dynamic Web Pages
5 PHP With MySQL (Gujarati) (Gujarati Edition)

PHP With MySQL (Gujarati) (Gujarati Edition)

BUY & SAVE
$7.00
PHP With MySQL (Gujarati) (Gujarati Edition)
+
ONE MORE?

In PHP, you can replace a symbol in a text string using the str_replace() function. The str_replace() function searches for a specified symbol or symbols within a string and replaces them with a new symbol. Here's an example of how to use str_replace() to replace a symbol in a text string:

$text = "Hello, World!"; $newText = str_replace(",", "!", $text);

echo $newText;

In this example, the str_replace() function replaces the comma symbol , with an exclamation mark !. So, the output will be Hello! World!.

You can replace any symbol, or even multiple symbols, by modifying the parameters of str_replace(). The first parameter is the symbol(s) you want to replace, the second parameter is the new symbol(s) you want to use, and the third parameter is the input string.

Hope this helps in understanding how to replace a symbol in a text string using PHP!

What is the str_replace array syntax for symbol replacement?

The str_replace function in PHP can be used to replace specific symbols with another symbol or string. The syntax for using an array as the replacement parameter in str_replace is as follows:

str_replace(array $search, array $replace, string|array $subject, int &$count = null): string|array|null

In this syntax, $search represents an array of symbols to be replaced, while $replace represents an array of symbols or strings to be used as replacements. $subject is the original string or array of strings where the symbols should be replaced.

Here's an example of using str_replace with an array for symbol replacement:

// Original string $originalString = "Hello, world!";

// Symbols to replace $search = array(",", "!");

// Symbols to use as replacements $replace = array("-", "?");

// Perform the replacement $newString = str_replace($search, $replace, $originalString);

// Output the result echo $newString;

Output:

Hello- world?

In the above example, the comma (,) and exclamation mark (!) in the original string are replaced with a hyphen (-) and a question mark (?), respectively.

How to replace symbols only within a specific portion of a string in PHP?

To replace symbols only within a specific portion of a string in PHP, you can use the substr_replace function along with regex pattern matching. Here's an example:

$string = "This is my string! @##$%&*^@# I want to replace symbols here."; $portion = "@##$%&*^@#"; // Specific portion of the string where symbols need to be replaced

// Define the replacement symbol $replacement = '*';

// Use regex pattern to match symbols within the portion of the string $pattern = '/[@#$%&*^]+/';

// Replace symbols within the portion using substr_replace and regex pattern matching $modifiedString = substr_replace($string, preg_replace($pattern, $replacement, $portion), strpos($string, $portion), strlen($portion));

echo $modifiedString;

Output:

This is my string! ********** I want to replace symbols here.

In this example, we define the original string and the specific portion of the string where symbols need to be replaced. We then define the replacement symbol and use a regex pattern to match symbols within the portion. Finally, we replace the symbols using substr_replace() and preg_replace(). The $modifiedString will contain the modified string with symbols replaced within the specified portion.

What is the str_ireplace function and how does it work?

The str_ireplace function is a built-in PHP function used to perform case-insensitive string replacement. It allows you to search for a specific substring within a string and replace all occurrences of that substring with a new value while ignoring the case.

The syntax for using str_ireplace is: string str_ireplace($search, $replace, $subject [, int &$count])

Parameters:

  • $search: The substring to search for (case-insensitive).
  • $replace: The new value to replace the search substring with.
  • $subject: The original string that will be searched and modified.
  • &$count (optional): A variable that will be updated with the number of replacements made.

The function returns a new string with all occurrences of the search substring replaced with the replacement text. If no occurrences of the search substring are found, the function returns the original string.

Here's an example usage:

$str = "Hello, World!"; $newStr = str_ireplace("world", "everyone", $str); echo $newStr;

Output:

Hello, everyone!

In this example, str_ireplace replaces the case-insensitive substring "world" with "everyone" in the original string "$str". The resulting new string is then printed.