skip to Main Content

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


  1. I think you this works for you

    <?php
    if (strpos($character['text'],'(PhraseToRemove)') == true) {
        $url = str_replace('(PhraseToRemove)', '', $character['text']);
        echo $url;  
    }
    ?>
    

    I change !== false to == true
    And $url = str_replace('(PhraseToRemove)', '', $url); to $url = str_replace('(PhraseToRemove)', '', $character['text']);

    Login or Signup to reply.
  2. There’s no need to check if PhraseToRemove is in text. If PhraseToRemove is in text – it will be removed, otherwise – text will not be changed:

    echo str_replace('(PhraseToRemove)', '', $character['text']);
    // or
    $character['text'] = str_replace('(PhraseToRemove)', '', $character['text']);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search