وظيفة PHP substr_replace()
التعريف والاستخدام
يستبدل جزءًا من النص بآخر باستخدام وظيفة substr_replace().
التعليقات:إذا start العدد المطلوب سالب length أقل أو يساوي start،ثم length للفئة 0.
التعليقات:هذه الوظيفة آمنة للحسابات الثنائية.
القواعد
substr_replace(string,replacement,start,length)
الم参数 | الوصف |
---|---|
string | مطلوب. يحدد النص الذي سيتم فحصه. |
replacement | مطلوب. يحدد النص الذي سيتم إدخاله. |
start |
مطلوب. يحدد الموضع الذي يبدأ منه الاستبدال في النص.
|
length |
Optional. Specifies how many characters to replace. The default is the same as the string length.
|
Technical Details
Return Value: | Returns the replaced string. If string If it is an array, it returns an array. |
PHP Version: | 4+ |
Update Log: | Since PHP 4.3.3, all parameters accept arrays. |
More Examples
Example 1
Replace starting from the 6th character of the string (replace "world" with "Shanghai"):
<?php echo substr_replace("Hello world","Shanghai",6); ?>
Example 2
Replace starting from the 5th character from the end of the string (replace "world" with "Shanghai"):
<?php echo substr_replace("Hello world","Shanghai",-5); ?>
Example 3
Insert "Hello" at the beginning of "world":
<?php echo substr_replace("world","Hello ",0,0); ?>
Example 4
Replace multiple strings at once. Replace "AAA" with "BBB" in each string:
<?php $replace = array("1: AAA","2: AAA","3: AAA"); echo implode("<br>",substr_replace($replace,'BBB',3,3)); ?>