logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ php str_replace

PHP STR_REPLACE

by Admin _ May 19, 2022

The str_replace() function is a case-sensitive, built-in function of PHP which replaces some character of the string with other characters. It is used khổng lồ replace all the occurrences of the tìm kiếm string with the replacement string.

Syntax

The syntax of the str_replace() function is given below, which has the following four parameters.

Bạn đang xem: Php str_replace


This function follows some rules while working, which are given below:

If the string which is to lớn be searched is an array, it returns an array.If the string which is lớn be searched is an array then search and replace is performed with each element of the array.If both $search và $replace are arrays and $replace has fewer elements than $search array, then an empty string will be used as replace.If the $search is an array, but $replace is a string, then the replace string will be used for every tìm kiếm value.

Parameter

The str_replace() function has four parameters in which three are mandatory, và the remaining one is an optional parameter. All these following parameters are described below:

$search (mandatory) - This parameter is a mandatory parameter that can have both string và array type values. The $search parameter contains the value which is going khổng lồ be searched for the replacement in the $string.

$replace (mandatory) - This parameter is a mandatory parameter which is replaced with the search values. In simple words - this parameter holds that value which will replace the $search value in the $string.

$string (mandatory) - This parameter is also a mandatory parameter which is an array or string in which tìm kiếm and replace value is searched and replaced. It is the string or array with which we are working.

$count (mandatory) - It is the last & optional parameter. It is an integer variable which counts the number of replacements done in the string. Simply, this variable stores the total number of replacement performed on a string $string.

Return Values

This function returns an array or a string with the replaced values which is based on the $string parameter.

Important Technical Details

Return valuesIt returns a string or an array with the replaced values.
Supported PHP versionPHP 4 & above versions support this function.

Xem thêm: Kpi Dashboard Là Gì ? Giải Pháp Tương Lai Cho Ngành Phân Tích Số Liệu Marketing

ChangelogThe $count parameter was included in PHP 5.0This function experienced many trouble while using both $search and $replace parameters as an array before PHP 4.3.3. So, that empty $search indexes to lớn be skipped without advancing the internal pointer on the $replace array. Newer versions have resolved this problem.After PHP 4.0.5, most of the parameters can now be an array.

Example

There is the practical implementation of the str_replace() function.

Example 1: Basic example with string variable


"."String before replacement:"."";echo $string."";$newstr = str_replace($search, $replace, $string, $count);echo ""."New replaced string is:"."";echo $newstr."";echo "Number of replacement =".$count;?>

Output:

In the above example, we can see that "Hii" is replaced with "Hello" and the number of replacement is only 1.

*
Note: We can pass the $search và $replace value directly in the str_replace() function.

Example 2: Replacement with array variable

To replace the multiple values in the $string, we have lớn take an array lớn store these values for replacement.


";echo $string."";$newstr = str_replace($search, $replace, $string, $count);echo ""."New replaced string is:"."";echo $newstr."";echo "Number of replacement =".$count;?>

Output:

In this output, we can see that "Hii" is replaced with "Hello" and "We" is replaced with "You" and the number of replacement is 2.

*

Example 3: Replacement of vowels with empty string


";echo $string."";$newstr = str_replace($search, "", $string, $count);echo ""."New replaced string is:"."";echo $newstr."";echo "Number of replacement =".$count;?>

Output:

In this example, we replace the vowels (both uppercase and lowercase) with the empty string. Here, 10 replacements are done in this string.

*

Example 4: Case-sensitive

The str_replace is a case-sensitive function, it is proved in the below example.


Output:

In this example, "Hello" is not replaced with "Hii" because the tìm kiếm string is "hello." In the second case "Hello" has replaced with "Hii" because here search string is "Hello" which matched in the string. It proves that str_replace() function is case-sensitive.

Share Tweet Linkedin Pinterest
Previous Post

Guide on php str_replace: learn to use str_replace function

Next Post

Regex php

CÙNG CHUYÊN MỤC

form register php

Form register php

09/04/2021
fantastic blog (cms) in php with source code

Fantastic blog (cms) in php with source code

28/04/2021
validate form php javascript

Validate form php javascript

28/04/2021
http diendanlequydon com viewtopic php style 6

Http diendanlequydon com viewtopic php style 6

28/04/2021
bản khai đăng ký tên miền

Bản khai đăng ký tên miền

02/07/2022
phần mềm lập trình php tốt nhất

Phần mềm lập trình php tốt nhất

01/07/2022
ghi file trong php

Ghi file trong php

30/06/2022
thực tập part time php hà nội

Thực tập part time php hà nội

30/06/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Upload image with preview and delete options

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2022 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.