pull-icon
logo-mobile

Not logged in

Your Account

Creating a Function

Home/Tutorials/Creating a Function

Creating a Function

Content by: Steve Stewart

Posted 10 months ago with 200 views

Simple function of bad words

Here we create a function containing an array of bad words we don't want to allow through our string.
For this demonstration I have three but this can be any number, just remember if you add more, you have to add more to the replace array.

<?php
function badWords($string){
    $words = array("bullets","dick","damn");
    $replace = array("snowboard","friend","amazing");
    $string = str_replace($words,$replace,$string);
    return $string;
}
$mystring = "Hi dick, damn you want to play with the bullets";
$cleaned = badWords($mystring);
echo $cleaned;