PHP replace

王林
發布: 2024-08-29 12:49:39
原創
831 人瀏覽過

replace() function is a function in PHP which mainly deals with strings and aims in replacing any string by performing a search for the string that needs to get replaced. The pattern of search for string can be in a way where either the entire search string needs to be replaced or it can be array that can get replaced by the searched replacement string in given string or array. replace() function is a build-in function in PHP which returns a new string or array with replaced values. This function accepts four parameters or arguments mandatorily namely search_val, replace_val, subject_val and count.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of PHP replace

Given below is the syntax :

str_replace(search_val, replace_val, subject_val, count)
登入後複製

The syntax flow is in a way where the parameters are represented as follows:

  • search_val:search_val parameter represents both the string and array type. This parameter specifies the string that needs to be searched and replaced by the searched string.
  • replace_val:replace_val parameter represents both the string and array type. This parameter specifies the string that needs to replace the string searched which is with the parameter search_val.
  • subject_val:subject_val parameter represents both string and array type which needs to replace with replace_val and search with search_val.
  • count:count is an optional value which is passed when the value gets set and replaced with the total number of replacement operations performed on string with subject_val.

How replace Function work in PHP?

replace() function in PHP is a build-in function which is used to replace all the possible parameters that need to get replace in the given string therefore it has some working criteria that needs to be followed:

  • Some mandatory function in PHP involves these parameters like search_val, replace_val, subject_val and count without which the working remains incomplete.
  • Some very important criteria to be kept in mind, like if search_val with string and replace_val with string gets searched in the subject_val string then gets substituted by replace_val arguments string of the corresponding string and its arguments.
  • If number of arguments in the element of replace_val comes out to be lesser than needed in search_val then it will be replacing the value or element with the empty string.
  • The other case which arise and says for replacement also can include array or string with some values.
  • Let’s consider the array or string with elements then the entire string will get searched within the subject_val.
  • Count value also plays a very important role simultaneous to search_val and replace_val in a way that this argument tries to get passed from the function and its value will set to total number of operations performed for replacement of strings and that string should be string with subject_val.
  • The return value for replace function() comes out to be a string i.e. a general string or the string can be string with all the occurrences of replaced value.
  • This function is considered as case sensitive function but when compared to str_ireplace() function the str_ireplace() function performs a case-insensitive search.
  • This function is considered binary safe as well.
  • This function supports in a full fledge mode in versions 4+.
  • The optional value of count has another significance which means count function is added in PHP version more than 5.0.
  • Earlier Versions of PHP less than 4 involves function which involves quite a lot of complex functions such as find and replace which was indeed making the entire function of string find and replace very cumbersome. It will cause empty find indexes and interplay of internal pointers of arrays.
  • All these complexities of find and replace got overcome with the count function being introduced in the PHP with versions 4+ .
  • Search functionality and replace function needs lot of attention and focus as both will be used internally in order to perform the functionality with respect to the search operation.
  • Sometimes there is a misconception between str_replace() method and str_ireplace() method but the difference is not much very minute difference lies with the fact that both seals with the case sensitive and case insensitive modes respectively.
  • Overall string_replace() function works with four mandate parameters which provides re-usability and flexibility to programmers at the time of implementation.

Examples of PHP replace

Given below are the examples mentioned :

Example #1

This program demonstrates the replace() function in PHP which first finds for the string and then replaces the value of the string with some part as defined. It makes the entire php string replaced with some value as shown in the output.

Code:

   

Let's Find for the string "Life_in_writing" and replace the value of writing with "Anusua"

登入後複製

Output

PHP replace

Example #2

This program demonstrates an array of fruit which is trying to replace the particular value of the fruit like guava string with apple as shown in the output.

Code:

   

Find the array with fruit guava and then substitute it with another fruit Apple.

" . "replaced_fruit: $k"; ?>
登入後複製

Output:

PHP replace

Example #3

This program demonstrates the substitution of string with elements and values of String with some subject_val string as the string value is less it will get substituted easily as show shown in the output.

Code:

     
登入後複製

Output:

PHP replace

Example 4

This program demonstrates the ireplace_string() which acts as case insensitive when compared with the replace() function as shown in the output.

Code:

   

find the string "All is good" and then make it replaced with the capital letter EVERYONE!

登入後複製

Output:

PHP replace

Conclusion

PHP replace() is a function which gives programmers flexibility and scope of re-usability at the time of execution and lets user to use function for searching and replacing string accordingly. It gives users a view of implementation in terms of requirement when it comes to adopting the functionality. Overall a function which plays a significant role in PHP.

以上是PHP replace的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!