PHP Tutorials

How to Replace String In PHP?

By definition, str_replace() utility, swaps some characters with others in a string. This means programmers and web designers can insert various characters in a string in order to achieve various results. This is the main essence of being able to replace strings in PHP sources. Every function has its own rules and ways of working. In the following PHP tutorial, we will talk about rules of this function.

  • If a string being searched is an array, it comes back as an array
  • If a string being searched is an array, the find and replace function is carried out with every array element
  • If the find and replace functions are arrays, and the replace function has a lower number of elements than find, it utilizes an empty string as replace
  • In a situation where find is an array and replace is a string, the replace string is utilized for each find value

These functions are what make this a highly effective and widely used source for PHP. Some important facts to note are that this function is binary safe. In addition, it is case sensitive. This is where its effectiveness is highlighted the most. In order to perform a case-insensitive search, users follow the following procedure Str replace()  In addition, users can utilize this technique str_ ireplace().  These methods are similar, but yet a bit different from each other.

The syntax for str_rplace reads, str_replace (find, replace, string, count) whereas, the str_ireplace syntax reads, str_ireplace (find, replace, string, count). Once again, these two are similar, but the difference comes in the use, which will be described later. Each function has its own parameters and the way it works. the parameters and descriptions are exactly the same in both PHP string functions. The following is a list of those parameters and their descriptions.

  • Find: it is required in the syntax and stipulates the values to find
  • Replace: it is required and it identifies the values to be replaced and the values in find
  • String: it is also required and it states the string supposed to be searched
  • Count: this is optional, because it is inconsistent and counts the number of replacements

On the part of technical details, these two functionalities differ quite variably. The following are the technical details:

Str_replace

Return value: it brings back a string or an array with the replaced values

PHP Version: 4+

Changelog: Some few changes were implemented to this function. The first change is the addition of the count parameter in PHP 5.0. This function was quite unstable and users experienced many problems with the use of arrays this was before PHP4.3.3. This problem brought about skipping of empty indexes without the replace array advancement. However, new versions will work smoothly and not have these problems. In addition, a new change is now most parameters can now be arrays in PHP 4.0.5.

Str_ireplace

Return value: replaces the values of a string or an array

PHP Version: 5+

Changelog: the only change to occur here is addition of the count parameter to PHP 5.0

Examples of functions

The following is an example of using the str_replace() function and the result thereof.

The PHP source reads as follows:

<!DOCTYPE html>
<html>
<body>
<?php
echo str_replace(“world”,”Brian”,”Hello world!”);
?>
<b>In this example, we search for the string “Hello World!”, find the value “world” and then replace the value with “Brian”.</b>
</body>
</html>

Here are the results of using this function.

Hello Brian!

In this example, we search for the string “Hello World!”, find the value “world” and then replace the value with “Brian”

Str_ireplace() function

The following is an illustration of how this function works.

PHP Source:


<!DOCTYPE html>
<html>
<body>
<?php
echo str_replace(“WORLD”,”Brian”.”Hello world!”);
?>
<b>In this example, we search for the string
“Hello World!”, find the value “World” and then replace the value “Brian”.</b>
</body>
</html>
 

Here are the results of this function:

Hello Peter!

In this example, we search for the string “Hello World!”, find the value “WORLD” and then replace the value with “Brian”.

In addition, there are more ways one can use these functions. This is why they are so versatile and widely used in the programming world. Users can use these functions in many ways. The example below demonstrates using a count variable and an array.


<?php
$arr=array(“blue”,”red”,”green”,”yellow”);
Print_r(str_replace(“red”,”pink”,$arr,$i));
Echo  “Replacements:  $i”;
?>

In the following example, this is a demonstration of using fewer elements in replace than find.


<?php
$find = array(“Hello”,”world”);
$replace = array(“B”);
$arr = array(“Hello”, “world”,”!”);
Print_r(str_replace($find,$replace,$arr));
?>

This is a reliable source code, with capabilities to give users the desired results and most importantly security. It is always important to have some security, as this keeps hackers and attackers away. This is by far one of the most effective string replacement tools. It lets users do whatever they may desire in creating the results they need.

Leave a Reply

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

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