0

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.

1
  • Making the replacement be a string ($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!'); Commented Mar 9, 2015 at 19:59

3 Answers 3

2

To not match parts of a word, you're going to need to use preg_replace().

Try something like this:

$rawstring = "Hello NONE. Your N/A is a pleasure to have! Your friend Johanna is also here.";
$placeholders = array('N/A', 'NA', 'NONE');

//Turn $placeholders into an array of regular expressions
//  `#` delimits the regular expression. Make sure this doesn't appear in $placeholders.
//  `(.+)` matches and captures any string of characters
//  `\b` matches word boundaries
//  `${1}` reinserts the captured pattern.
//  `i` at the end makes this case insensitive.
$re = preg_replace('#(.+)#', '#\b${1}\b#i', $placeholders); 

//Make the same substitution for all matches.
$substitution = '';

$greeting = preg_replace($re, $substitution, $rawstring);
echo  $greeting;
Sign up to request clarification or add additional context in comments.

1 Comment

I like what you've done there. Very succinct replace mechanism and also appreciate the comments explaining the $re variable.
0

I'd look at http://php.net/sprintf if you already know the string and are just looking to sub in certain spots. Sorry for the answer I don't have enough rep yet to comment.

Comments

0

I'd set up an associative array to set up the vars for replace. - edit, had to escape the slash in N/A

$val_to_swap_in = '';
$replacers = array(
    '/NONE/' => $val_to_swap_in,
    '/N\/A/' => $val_to_swap_in, 
    '/NA/'   => $val_to_swap_in,
);
$greeting = preg_replace(array_keys($replacers), array_values($replacers),  $rawstring);

Resulted in this in php cli shell:

Hello . Your  is a pleasure to have! Your friend Johanna is also here.

1 Comment

Unfortunately, I don't have any control over the incoming string. I cannot replace the tokens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.