Tags: PHP
I'm going to try to post "useful thingies" in this blog. These are helper functions which I include on and off into my projects. Here are a couple of short and sweet functions that have saved me countless minutes of typing.
function br2nl ($string) {
return preg_replace('//i', "n", $string);
}
function url_safe ($string){
$string = preg_replace('/[^a-zA-Z0-9_ -]/s', '', $string);
$string = preg_replace("![^a-z0-9]+!i", "-", $string);
return $string;
}
function random_string ($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
©2023, kirillsimin.com