PHP substr_replace() Function

P
PHP substr_count() Function
PHP trim() Function

what is the syntax of the UBSTR_REPLACE() function in php?

substr_replace(string,replacement,start,length)
ParameterDescription
stringRequired. Specifies the string to check
replacementRequired. Specifies the string to insert
startRequired. Specifies where to start replacing in the stringA positive number – Start replacing at the specified position in the stringNegative number – Start replacing at the specified position from the end of the string0 – Start replacing at the first character in the string
lengthOptional. Specifies how many characters should be replaced. Default is the same length as the string.A positive number – The length of string to be replacedA negative number – How many characters should be left at end of string after replacing0 – Insert instead of replace
PHP UBSTR_REPLACE() method

examples of the UBSTR_REPLACE() function

Example 1. In this example, we replace “Hello” with “world”.

<?php
echo substr_replace("Hello","world",0);
?>

Example 2. In this example, we start replacing at the 6th position in the string (replace “world” with “earth”).

<?php
echo substr_replace("Hello world","earth",6);
?>

Example 3. In this example, we start replacing at the 5th position from the end of the string (replace “world” with “earth”).

<?php
echo substr_replace("Hello world","earth",-5);
?>

Example 4. In this example, we insert “Hello” at the beginning of “world”.

<?php
echo substr_replace("world","Hello ",0,0);
?>

Example 5. In this example, we replace multiple strings at once. Replace “AAA” in each string with “BBB”.

<?php
$replace = array("1: AAA","2: AAA","3: AAA");
echo implode("<br>",substr_replace($replace,'BBB',3,3));
?>
PHP substr_count() Function
PHP trim() Function

Add comment

Tutor Network

Share this

Learn PHP from A to Z