PHP str_replace() Function

PHP str_replace() Function

The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive.

Syntax

str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

Parameter

search(required)- This parameter signifies the value to search.

replace(required)- This parameter represents the replacement value that replaces found search values.

subject(required)- This parameter specifies the string to be searched.

count(optional)- This parameter signifies a variable that counts the number of replacements

Return

This function returns a string or an array with the replaced values.

Example 1

 

Output

The original string:Hello World!
After the str_replace() function...
New string: Hello world! 

Example 2

   

Output

After using the str_replace()...
 Array
 (
     [0] => T
     [1] => and
 ) 

Example 3

 

Output

Array
 (
     [0] => Hello
     [1] => world
     [2] => $
 ) 

Related Topics

PHP natcasesort() Function

PHP natcasesort() Function The natcasesort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natcasesort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean...

1 minute read.

PHP quoted_printable_decode() Function

PHP quoted_printable_decode() Function The quote_printable_decode() function of PHP converts a quoted-printable string to an 8 bit string. It works similar to imap_qprint(), except this one does not require the IMAP module to work. Syntax quoted_printable_decode ( string $str ) Parameter str(required)- This parameter represents the...

1 minute read.

PHP unserialize() Function

PHP unserialize() Function The unserialize() function in PHP takes a single serialized variable and converts it back into a PHP value. Syntax unserialize ( string $str [, array $options ] ) Parameter value(required)- This parameter represents the serialized string. options(optional)- This parameter signifies any options to be...

1 minute read.

PHP is_string() Function

PHP is_string() Function The is_ string() function in PHP finds whether a variable is a string or not. It returns a Boolean value TRUE if the parameter var is string, else it returns FALSE. Syntax is_string ( mixed $var ) Parameter var(required)- This parameter represents...

1 minute read.

PHP strlen() Function

PHP strlen() Function The strlen() function in PHP is used to return the length of the specified string. Syntax strlen ( string $string ) Parameter string(required)- This parameter represents the string which is measured for length. Return This function returns the length of the string on success or returns 0 if the specified...

1 minute read.

PHP array_chunk() Function

PHP array_chunk() Function The array_chunk() function in PHP is used to split an array into chunks where the last chunk may contain less than size elements. Syntax array_chunk ( array $array , int $size [, bool $preserve_keys ]...

1 minute read.

PHP ord() Function

PHP ord() Function The ord() function in PHP converts the first byte of a string to a value between 0 and 255. This function interprets the binary value of the first byte of string as an...

1 minute read.

PHP Variables

PHP Variable is the name of memory location used to hold the data.  In PHP, variable starts with $ symbol. Rules for PHP Variables. Variable starts with $ sign. Variable name...

2 minutes read.

PHP str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

2 minutes read.

PHP is_int() Function

The PHP is_int() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it...

1 minute read.

PHP Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes read.

PHP strnatcmp() Function

PHP strnatcmp() Function The strnatcmp() function in PHP compares two strings using a "natural" algorithm. It is case-sensitive. Syntax strnatcmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than(...

1 minute read.

PHP strval() function

PHP strval() function The strval() function in PHP is used to get the string value for the specified variable. This function cannot be used on arrays or objects, if applied then would return only the...

1 minute read.

PHP strrchr() Function

PHP strrchr() Function The strrchr() function in PHP finds the last occurrence of a character in the specified string and returns the portion of haystack, which starts at the last occurrence of the needle and goes until the end of haystack....

1 minute read.

PHP fprintf() Function

PHP fprintf() Function The fprintf() function in PHP outputs a formatted string. This function operates as printf() but accepts an array of arguments, rather than a variable number of arguments. Syntax vprintf ( string $format , array $args ) Parameter format(required)- This parameter specifies the string and how to...

4 minutes read.

Python if-else

Python if-else The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value. If the conditional expression...

2 minutes read.

PHP MySQL Database Tutorial

MySQL is the most popular database system that is used within PHP. Let's first understand about MySQL. What is MySQL? MySQL is open-source Relational Database Management System (RDMS). The name of MySQL is...

13 minutes read.

PHP str_word_count() Function

PHP str_word_count() Function The str_word_count() function in PHP counts the number of words inside the string and returns the information about words used in a string. String str_word_count( string $string [, int $format [, string $charlist ]] ) Parameter string(required)- This parameter represents the input string. format(required)- This parameterspecifies the...

2 minutes read.

PHP str_ireplace() Function

PHP str_ireplace() Function The PHP str_ireplace() function in PHP replaces some characters with some other characters in a string. Syntax str_ireplace ( mixed $search , mixed  $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter specifies the value being searched for. replace(required)- This parameter represents the replacement value...

1 minute read.

PHP substr_compare() Function

PHP substr_compare() Function The substr_compare() function in PHP compares two strings from a specified start position. It is a binary-safe and optionally case-sensitive function. Syntax substr_compare ( string $main_str , string $str , int $offset [, int $length [, bool$case_insensitivity]] )  Parameter main_str(required)- This parameter specifies the first string to compare. str(required)- This parameter...

1 minute read.