I'm trying to replace several possible strings with only one in PHP. Also, the strings must match only a complete word:
<?
$rawstring = "Hello NONE. Your N/A is a pleasure to have! Your friend Johanna is also here.";
//strings for substitution
$placeholders = array('N/A', 'NA', 'NONE');
//replace with blank spaces.
$substitution = array('');
$greeting = str_ireplace($placeholders, $substitution, $rawstring);
echo $greeting . "<br />";
?>
This is the resulting string:
Hello . Your is a pleasure to have! Your friend Johan is also here.
This is almost the output I'm looking for. I would like the substitution to only affect individual words. In this case, it's replacing the 'na' in 'Johanna', resulting in 'Johan'. It should still print out 'Johanna' .
Is this possible?
EDIT: I cannot control $rawstring. This is just an example.
$substitution = '';) will do the replacement for you. The more tricky part is only replacing "whole words". For that, a common approach is use a regular expression and word boundaries, like$result = preg_replace('/\bSQUIRREL\b/i', 'kitty', 'Oh look, a squirrel!');