Skip to main content

Search Function Display

Nov 18, 2022AustinLeath
Loading...

More PHP Posts

List Files in Directory

Nov 18, 2022AustinLeath

0 likes • 1 view

<?php
$listOfFiles = glob("*.{jpg,png,gif,tiff,jpeg}", GLOB_BRACE);
echo 'images = []; ';
for($x = 0; $x <= count($listOfFiles)-1; $x++) {
echo "images[" . $x . "] =" . '"' . $listOfFiles[$x] . '"' . "; ";
}
?>

preg_match() username sanitization

Nov 18, 2022AustinLeath

0 likes • 0 views

<?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
?>

Access Associative Array

Nov 18, 2022AustinLeath

0 likes • 1 view

<?php
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
// this cycle echoes all associative array
// key where value equals "apple"
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key($array), "\n";
}
next($array);
}
?>

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();
}
}
?>

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();
?>

Generate 64K edges

Nov 18, 2022AustinLeath

0 likes • 4 views

<?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>";
}
?>