Skip to main content

Search Function Display

Nov 18, 2022AustinLeath
Loading...

More PHP Posts

Make Random Number

Nov 19, 2022CodeCatch

0 likes • 0 views

<?php
function getRandomId($min = NULL, $max = NULL) {
if (is_numeric($min) && is_numeric($max)) {
return mt_rand($min, $max);
}
else {
return mt_rand();
}
}
?>

ID Generator

Nov 18, 2022AustinLeath

0 likes • 0 views

<?php
function uniqidReal($length = 8) {
// uniqid gives 8 chars, can be changed
if (function_exists("random_bytes")) {
$bytes = random_bytes(ceil($length / 2));
} elseif (function_exists("openssl_random_pseudo_bytes")) {
$bytes = openssl_random_pseudo_bytes(ceil($length / 2));
} else {
throw new Exception("no cryptographically secure random function available");
}
return substr(bin2hex($bytes), 0, $length);
}
/*
//for testing
for($i = 0; $i < 10; $i++) {
echo "<br>";
echo uniqidReal();
}
*/
?>

Get File Extension

Nov 19, 2022CodeCatch

0 likes • 0 views

<?php
function getFileExtension($file) {
return end(explode(".", $file));
}
?>

Crop an image

Nov 19, 2022CodeCatch

0 likes • 0 views

<?php
$im = imagecreatefrompng('example.png');
$size = min(imagesx($im), imagesy($im));
$im2 = imagecrop($im, ['x' => 0, 'y' => 0, 'width' => $size, 'height' => $size]);
if ($im2 !== FALSE) {
imagepng($im2, 'example-cropped.png');
imagedestroy($im2);
}
imagedestroy($im);
?>

Parse JSON

Nov 19, 2022CodeCatch

0 likes • 0 views

<?php
$json ='{"id":1,"name":"foo","interest":["wordpress","php"]}';
$obj=json_decode($json);
echo $obj->interest[1]; //prints php
?>

RGBA Generator

Nov 18, 2022AustinLeath

0 likes • 0 views

<?php
//generate 239 rgba colors for codecatch's 239 supported programming languages!
//this was used to generate the colors that are used in the pie chart on https://codecatch.net/graphs.php
function rgbagenerator() {
$count = 239;
for ($i=0; $i < $count; $i++) {
echo "'rgba(". mt_rand(0,255) . ",". mt_rand(0,255) . ",". mt_rand(0,255) . ",1)',";
echo "<br>";
}
}
rgbagenerator();
?>