jiros1 Posted December 3, 2015 Share Posted December 3, 2015 (edited) 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 December 3, 2015 by jiros1 Quote Link to comment Share on other sites More sharing options...
Barand Posted December 3, 2015 Share Posted December 3, 2015 At what point in the script are inserting the F word? Because $game is reset to Dragonball3 when you execute the findOrFail() method above. Quote Link to comment Share on other sites More sharing options...
jiros1 Posted December 8, 2015 Author Share Posted December 8, 2015 Thanks for the reply, I was trying this in Laravel and it didn't work, now I've fiddled with it in "vanilla" PHP and now it works. I guess it was too soon for me to try something in a framework I haven't done yet. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted December 8, 2015 Share Posted December 8, 2015 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.