replace() 함수는 주로 문자열을 다루고 대체해야 하는 문자열을 검색하여 모든 문자열을 바꾸는 것을 목표로 하는 PHP의 함수입니다. 문자열 검색 패턴은 전체 검색 문자열을 바꿔야 하는 방식일 수도 있고, 주어진 문자열이나 배열에서 검색된 대체 문자열로 바꿀 수 있는 배열일 수도 있습니다. 교체() 함수는 교체된 값이 포함된 새 문자열이나 배열을 반환하는 PHP 내장 함수입니다. 이 함수는 네 개의 매개변수 또는 인수, 즉 search_val, replacement_val, subject_val 및 count를 필수로 받아들입니다.
광고
이 카테고리에서 인기 있는 강좌
PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사
무료 소프트웨어 개발 과정 시작
웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등
PHP 대체 구문
다음은 구문입니다.
str_replace(search_val, replace_val, subject_val, count)
구문 흐름은 매개변수가 다음과 같이 표현되는 방식입니다.
-
search_val: search_val 매개변수는 문자열 유형과 배열 유형을 모두 나타냅니다. 이 매개변수는 검색하고 검색된 문자열로 바꿔야 하는 문자열을 지정합니다.
-
replace_val: replacement_val 매개변수는 문자열 유형과 배열 유형을 모두 나타냅니다. 이 매개변수는 검색된 문자열을 search_val 매개변수로 대체해야 하는 문자열을 지정합니다.
-
subject_val: subject_val 매개변수는 문자열과 배열 유형을 모두 나타내며, replacement_val로 바꾸고 search_val로 검색해야 합니다.
-
count: count는 값이 설정될 때 전달되고 subject_val을 사용하여 문자열에 대해 수행된 총 교체 작업 수로 대체되는 선택적 값입니다.
PHP에서 함수 교체는 어떻게 작동하나요?
PHP의 replace() 함수는 주어진 문자열에서 대체해야 하는 모든 가능한 매개변수를 바꾸는 데 사용되는 내장 함수이므로 따라야 할 몇 가지 작업 기준이 있습니다.
- 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:
<!DOCTYPE html>
<html>
<body>
<p>Let's Find for the string "Life_in_writing" and replace the value of writing with "Anusua"</p>
<?php
echo str_replace("writing","Anusua","Life_in_writing!");
?>
</body>
</html>
Output
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:
<!DOCTYPE html>
<html>
<body>
<p>Find the array with fruit guava and then substitute it with another fruit Apple.</p>
<?php
$ar_ray = array("guava","kiwi","apple","orange");
print_r(str_replace("guava","apple",$ar_ray,$k));
echo "<br>" . "replaced_fruit: $k";
?>
</body>
</html>
Output:
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:
<!DOCTYPE html>
<html>
<body>
<?php
$search = array("Welcome","Everyone!");
$replace_str = array("Zee");
$arr_ay = array("Welcome","All",":)");
print_r(str_replace($search,$replace_str,$arr_ay));
?>
</body>
</html>
Output:
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:
<!DOCTYPE html>
<html>
<body>
<p>find the string "All is good" and then make it replaced with the capital letter EVERYONE!</p>
<?php
echo str_ireplace("EVERYONE!","Anusua","All is good");
?>
</body>
</html>
Output:
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 교체의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!