• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Seegatesite Header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • My Tools
    • Bootstrap Navbar Online Generator
    • Bootstrap Demo
    • Amazon Asin Grabber
    • Azoncast v2 Theme
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
🏠 » PHP » How To Replace String With Another String In PHP

How To Replace String With Another String In PHP

By Sigit Prasetya Nugroho ∙ September 16, 2019 ∙ PHP ∙ Leave a Comment

PHP has the function to replace all words or letters in a string or sentence with a str_replace() command. This PHP string reference used to replace words in a paragraph on a web page quickly and easily.

How To Replace String With Another String In PHP Using Str Replace

Table of Contents

  • 1 What is str_replace function in PHP?
  • 2 str_replace()
    • 2.1 PHP Arrays in str_replace()
  • 3 str_ireplace()
  • 4 Conclusion

What is str_replace function in PHP?

I quoted from https://www.php.net, str_replace() is a built-in function in PHP that used to replace all search strings with a replacement string.

str_replace()

The supported PHP versions are PHP 4, PHP 5 and PHP 7

Syntax:

1
str_replace ($search, $replace, $original_string)

str_replace has 3 parameters. These parameters are requested for the function to work correctly.

$search : the search strings used to find the string that we want to change. This parameter can contain strings or also in the form of Array.

$replace : All words found replaced with this string replace in this parameter.

$original_string : source string or paragraph you want to change.

Following example :

1
2
3
4
$original_string = "I love pizza";
$search = "pizza";
$replace = "burger";
echo str_replace($search ,$replace, $original_string );

Replace Strings In Php Using Str Replace Function

With the example above, you have changed the word “pizza” to “burger.” The example above is function to replace a single string.

So what if we want to change multiple replacements at once in one str_replace function? the answer is to use an PHP arrays

PHP Arrays in str_replace()

You can search and replace multiple replacements strings at once using an array. See the following example.

Example 1

We search and replace many strings with a string in a paragraph.

1
2
3
4
$original_string = "i' love* seegatesite.com~ to get^ any tutorial";
$search = array("'",'*', '~', '^');
$replace = "";
echo str_replace($search ,$replace, $original_string );

Using Array And Replace With String In php StrReplace

Example 2

We make 2 arrays, which function as search and replace.

1
2
3
4
$originalString = "My name is Sigit! I'm programmer who love PHP.";
$search = array('programmer','PHP');
$replace = array('Police','Cookies');
echo str_replace($search ,$replace, $originalString );

Example Searched Is An Array And Replaced Is An Array Str Replace PHP Min

The example above, the searched is an array and the replaced is an array too.

Note:

The str_replace() function is case-sensitive.

If you want to replace words in a string both uppercase and lowercase, you can use the str_ireplace() function. How to use str_ireplace() is the same as str_replace().

str_ireplace()

The supported PHP versions are PHP 5 and PHP 7

Example

1
2
3
4
$originalString = "I love PizZa" ;
$search = 'pizza';
$replace =  'burger';
$result =  str_ireplace($search ,$replace, $originalString );

Many interesting examples of using PHP strreplace can be found on the PHP official site. One of them is my favorite as follows.

Convert foreign 8859-1 characters into HTML entities

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Convert foreign 8859-1 characters into HTML entities.
*
* @param string $str
*   The string being parsed.
*
* @return string
*   The converted string.
*/
public static function convert_chars_to_entities( $str )
{
    $str = str_replace( 'À', 'À', $str );
    $str = str_replace( 'Á', 'Á', $str );
    $str = str_replace( 'Â', 'Â', $str );
    $str = str_replace( 'Ã', 'Ã', $str );
    $str = str_replace( 'Ä', 'Ä', $str );
    $str = str_replace( 'Å', 'Å', $str );
    $str = str_replace( 'Æ', 'Æ', $str );
    $str = str_replace( 'Ç', 'Ç', $str );
    $str = str_replace( 'È', 'È', $str );
    $str = str_replace( 'É', 'É', $str );
    $str = str_replace( 'Ê', 'Ê', $str );
    $str = str_replace( 'Ë', 'Ë', $str );
    $str = str_replace( 'Ì', 'Ì', $str );
    $str = str_replace( 'Í', 'Í', $str );
    $str = str_replace( 'Î', 'Î', $str );
    $str = str_replace( 'Ï', 'Ï', $str );
    $str = str_replace( 'Ð', 'Ð', $str );
    $str = str_replace( 'Ñ', 'Ñ', $str );
    $str = str_replace( 'Ò', 'Ò', $str );
    $str = str_replace( 'Ó', 'Ó', $str );
    $str = str_replace( 'Ô', 'Ô', $str );
    $str = str_replace( 'Õ', 'Õ', $str );
    $str = str_replace( 'Ö', 'Ö', $str );
    $str = str_replace( '×', '×', $str );  
    $str = str_replace( 'Ø', 'Ø', $str );
    $str = str_replace( 'Ù', 'Ù', $str );
    $str = str_replace( 'Ú', 'Ú', $str );
    $str = str_replace( 'Û', 'Û', $str );
    $str = str_replace( 'Ü', 'Ü', $str );
    $str = str_replace( 'Ý', 'Ý', $str );
    $str = str_replace( 'Þ', 'Þ', $str );
    $str = str_replace( 'ß', 'ß', $str );
    $str = str_replace( 'à', 'à', $str );
    $str = str_replace( 'á', 'á', $str );
    $str = str_replace( 'â', 'â', $str );
    $str = str_replace( 'ã', 'ã', $str );
    $str = str_replace( 'ä', 'ä', $str );
    $str = str_replace( 'å', 'å', $str );
    $str = str_replace( 'æ', 'æ', $str );
    $str = str_replace( 'ç', 'ç', $str );
    $str = str_replace( 'è', 'è', $str );
    $str = str_replace( 'é', 'é', $str );
    $str = str_replace( 'ê', 'ê', $str );
    $str = str_replace( 'ë', 'ë', $str );
    $str = str_replace( 'ì', 'ì', $str );
    $str = str_replace( 'í', 'í', $str );
    $str = str_replace( 'î', 'î', $str );
    $str = str_replace( 'ï', 'ï', $str );
    $str = str_replace( 'ð', 'ð', $str );
    $str = str_replace( 'ñ', 'ñ', $str );
    $str = str_replace( 'ò', 'ò', $str );
    $str = str_replace( 'ó', 'ó', $str );
    $str = str_replace( 'ô', 'ô', $str );
    $str = str_replace( 'õ', 'õ', $str );
    $str = str_replace( 'ö', 'ö', $str );
    $str = str_replace( '÷', '÷', $str );  
    $str = str_replace( 'ø', 'ø', $str );
    $str = str_replace( 'ù', 'ù', $str );
    $str = str_replace( 'ú', 'ú', $str );
    $str = str_replace( 'û', 'û', $str );
    $str = str_replace( 'ü', 'ü', $str );
    $str = str_replace( 'ý', 'ý', $str );
    $str = str_replace( 'þ', 'þ', $str );
    $str = str_replace( 'ÿ', 'ÿ', $str );
  
    return $str;
}

Another tutorial : How To Send And Retrieve JSON Data PHP CURL Post Example

Conclusion

str_replace() is built-in string functions to replace word in string rapidly.  instead of using string variables, you can do multiple search strings using array and replace with another string.

You don’t need to bother making regular expressions to replace word in strings.

Thus my short tutorial on how to use PHP string replace, hopefully, useful

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Do you want to get Free WordPress Videos, Plugins, and Other Useful Resources ?

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

we respect your privacy and take protecting it seriously

Popular Articles

How To Replace String With Another String In PHP

September 16, 2019 By Sigit Prasetya Nugroho Leave a Comment

Increase Traffic with the Right Yoast Settings

October 16, 2014 By Sigit Prasetya Nugroho Leave a Comment

Boost Social Media Traffic with OnePress Social Locker

October 17, 2014 By Sigit Prasetya Nugroho Leave a Comment

Amazon Custom Thumbnail Plugin New Version Launch

September 18, 2014 By Sigit Prasetya Nugroho Leave a Comment

Web Applications as Desktop Flavor with Ajax Agent

October 11, 2014 By Sigit Prasetya Nugroho Leave a Comment

Tags

adminlte adsense adsense tips affiliate amazon ajax amazon Android angular angular 4 angular 5 asin grabber Bootstrap codeigniter create wordpress theme crud css free wordpress theme google adsense imacros increase traffic jquery laravel laravel 5 learn android modal dialog mysql nodeJs optimize seo pdo php plugin pos Publisher Tips SEO theme tutorial tutorial angular tutorial angular 4 tutorial javascript tutorial javascript beginners twitter widget wordpress wordpress plugin XMLRPC




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2019 Seegatesite.com