I am looking for a way to review the contents of a PHP string, replace a key phrase if included and then echo the updated string with the replacement in place.
The below looks at the content of the $character['text']
string and removes PhraseToRemove
from it – but I cant manage to echo the updated version of $character['text']
Can anyone help me out?
<?php
if (strpos($character['text'],'(PhraseToRemove)') !== false) {
$url = str_replace('(PhraseToRemove)', '', $url);
}
?>
2
Answers
I think you this works for you
I change
!== false
to== true
And
$url = str_replace('(PhraseToRemove)', '', $url);
to$url = str_replace('(PhraseToRemove)', '', $character['text']);
There’s no need to check if
PhraseToRemove
is in text. IfPhraseToRemove
is in text – it will be removed, otherwise – text will not be changed: