Jump to content

How to censor bad words with str_replace


jiros1

Recommended Posts

       
I'm trying to make a basic filter script for editing my games with str_replace.
I want to censor bad words with str_replace, but it's not working, it just returns the original input.
 

This is the contents of my function, it's activated when I submit the form:

        $game = Game::findOrFail($id);
        $filtered_game = str_replace('thefword', 'f***', $game);
        echo $filtered_game;

$game contains an array with 1 item: 

{"id":18,"name":"Dragonball3","category_id":2,"user_id":1,"created_at":"2015-12-03 11:35:04","updated_at":"2015-12-03 11:35:44","gamegroup_id":0}

The original game name is "dragonball3", when I put in the "F" word, my string won't be returned as "f***" but still as Dragonball3.

This function is originally intended for the edit functionality, which works perfectly but I have disabled this for testing purposes.

 

The output I want is this:

{"id":18,"name":"f***","category_id":2,"user_id":1,"created_at":"2015-12-03 11:35:04","updated_at":"2015-12-03 11:35:44","gamegroup_id":0}

Maybe there is a better way? I want to fix this but I think it's more important that I learn from this.

Edited by jiros1
Link to comment
Share on other sites

the findOrFail method in laravel returns a model, not a string. You can't use str_replace on a model, that doesn't really make sense. You would want to do a str_replace on the name attribute of the game model IE

 

$game = Game::findOrFail($id);
$name = $game->name;
$newname = str_replace("fword", "f***", $name);
$game->name = $newname;
$game->save();

 

without any context though, I'm not entirely sure this is what you want.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.