Skip to main content

Get File Extension

0 likes • Nov 19, 2022 • 0 views
PHP
Loading...

More PHP Posts

preg_match() username sanitization

0 likes • Nov 18, 2022 • 0 views
PHP
<?php
$username = "Austin Leath -_#";
$username_err = "no error";
if(preg_match('/[^a-z_\-0-9]/i', $string)) {
$username_err = "Username can only contain alphanumeric characters, dashes, and underscores.";
}
echo $username_err
?>

RGBA Generator

0 likes • Nov 18, 2022 • 0 views
PHP
<?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();
?>

Make Random Number

0 likes • Nov 19, 2022 • 0 views
PHP
<?php
function getRandomId($min = NULL, $max = NULL) {
if (is_numeric($min) && is_numeric($max)) {
return mt_rand($min, $max);
}
else {
return mt_rand();
}
}
?>

Crop an image

0 likes • Nov 19, 2022 • 0 views
PHP
<?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);
?>

ID Generator

0 likes • Nov 18, 2022 • 0 views
PHP
<?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();
}
*/
?>

Generate 64K edges

0 likes • Nov 18, 2022 • 2 views
PHP
<?php
header('Content-Type:text/html');
$jsonData = json_decode(file_get_contents("names.json"));
$namesArray = array();
foreach($jsonData as $data) {
$namesArray[] = $data;
}
for($i = 0; $i < 64000; $i++) {
echo 'D.add_edge("'. $namesArray[array_rand($namesArray)] . '", "' . $namesArray[array_rand($namesArray)]. '")';
echo "<br>";
}
?>