Skip to main content

str_replace_assoc()

public function str_replace_assoc( array $replace, string $subject )

This method is used to replace strings found in an associative array. This method was created from Wes Foster's strReplaceAssoc() function (source).


Arguments#

  • $replace
    (array) (required) The associative array containing strings to search for (the array keys) and the strings to replace matches with (the values)

  • $subject
    (string) (required) The string to process.


Return#

This method will return the passed string, but with any array key matches replaced by the associated values.


Example#

$truth = "I really like muffins.";
$words = array(
'really' => 'somewhat',
'like' => 'enjoy',
'muffins' => 'cake',
);
$this->str_replace_assoc( $words, $truth );
echo $truth;
// Result: "I somewhat enjoy cake."