The Snippets Thread
08-15-2011, 12:00 PM, (This post was last modified: 08-15-2011, 12:02 PM by MyDigitalpoint.)
#1
The Snippets Thread
Based on my own experience, snippets are a great way to learn PHP and sometimes a useful time-saver even if you know it because you can refer to these chunks of code for a quick integration into your coding.

Let's post small snippets that can be useful to other members whether to learn or add extra functionality to a website or blog, in example, WordPress using the PHP executable plugin.

The following snippet it's a directory listing so people can browse a directory on your site and even download files when you have setup a restriction via htaccess that does not allow directory listing:
[/quote]
Code:
<?php
$sd = '.';
$dir = opendir($sd);
echo '<table cellspacing="2" cellpadding="2">
<tr><td><b>Directory Name</b></td><td><b>Dir Type</b></td><td><b>Last Modified</b></td></td>';
while($item = readdir($dir)){
if( (is_dir($item)) && (substr($item, 0, 1) !='.') ){
$dt = filetype($item);
$lm = date('F j, Y', filemtime($item));
echo '<tr><td><a href="./'.$item.'">'.$item.'</a></td><td>'.$dt.'</td><td>'.$lm.'</td></tr>';
}
}
echo '</table><hr />';
rewinddir($dir);

echo '<table cellspacing="2" cellpadding="2">
<tr><td><b>File Name</b></td><td><b>File Type</b></td><td><b>Size</b></td><td><b>Last Mod.</b></td></tr>';
while($item = readdir($dir)){
if( (is_file($item)) && (substr($item, 0, 1) !='.') ){
$dt = filetype($item);
$lm = date('F j, Y', filemtime($item));
$fs = filesize($item);
echo '<tr><td><a href="./'.$item.'">'.$item.'</a></td><td>'.$dt.'</td><td>'.$fs.' bytes</td><td>'.$lm.'</td></tr>';
}
}
echo '</table>';
closedir($dir);
?>
MyDigitalpoint PRO Freelance Marketplace
Reply
10-02-2011, 11:35 PM,
#2
RE: The Snippets Thread
This snippet is supposed to connect you to a mysql server and database using the basic mysql functions.
PHP Code:
<?php 
$config
['mysqluser'] = "USERNAME";
$config['mysqlpass'] = "PASSWORD";
$config['mysqldatabase'] = "DATABASE";
$config['mysqlhost'] = "HOST";

mysql_connect($config['mysqlhost'],$config['mysqluser'],$config['mysqlpass']) or die(mysql_error());
mysql_select_db($config['mysqldatabase']) or die(mysql_error()); 
With love,
HiddenKnowledge
A.k.a. Yoruichi Shihouin

If you have any questions, feel free to contact me trough email or pm. :)
Reply
10-20-2011, 08:24 AM,
#3
RE: The Snippets Thread
This snippet generates random hexadecimal number to get HTML colors

PHP Code:
<?php 
function get_random_color() 

    for (
$i 0$i<6$i++) 
    { 
        
$color .=  dechex(rand(0,15)); 
    } 
    return 
"$color"

?>

This can be useful to randomize the background color of a website page or a section, such as the header or navigation bars this way:


PHP Code:
<div style="background:#<?php echo $color; ?>;">

or <
body background="#<?php echo $color; ?>;"
MyDigitalpoint PRO Freelance Marketplace
Reply
10-20-2011, 09:37 AM,
#4
RE: The Snippets Thread
Here's one of the ones I find extremely useful when parsing user input. It's a regex function that takes all non-numeric characters out of a string. [source]
PHP Code:
preg_replace('/\D/'''$string
Of course, you can replace the D with other regex strings, like [a-zA-Z] to remove all non alphabetic characters, or [a-zA-Z0-9] to remove all alphanumeric characters.
Hi! I'm Zach, and I founded NiftyHost. If you need anything, just PM me! :)
Reply
10-27-2011, 01:40 PM, (This post was last modified: 10-27-2011, 09:02 PM by Arthur.)
#5
RE: The Snippets Thread
When I saw this thread, I thought "Oh! I'll post my hex color code generator!", but obviously that would be pointless, as there already is one. So I'll post my snippet to delete all files in a directory (useful if you want to have a temp folder and it gets full of non-deleted useless files.)
PHP Code:
function empty_folder($folder){
    
$files glob("$folder/*");
    for (
$i 0$i count($files); $i++){
        
unlink($files[$i]);
    }


Function to highlight a string where $str is the string to be highlighted, $bg is whether it's the background or the text that's highlighted, and $color is what color to highlight it:
PHP Code:
function highlight($str$bg=true$color=false){
    
$type "";
    if (!
$color){
        
$color "#8B8B00";
    }
    if (
$bg){
        
$type "background-";
    }
    return 
"<span style='".$type."color:$color;'>".$str."</span>";

Reply
10-27-2011, 02:42 PM,
#6
RE: The Snippets Thread
@Arthur: You might have some trouble with your first function. empty() is a pre-defined function in PHP.
Hi! I'm Zach, and I founded NiftyHost. If you need anything, just PM me! :)
Reply
10-27-2011, 09:01 PM,
#7
RE: The Snippets Thread
Oops. I posted a modified version of the function. The version I actually use is called delete_temp(). So I'll change the function name in my previous post.
Thanks for noticing!
Reply
01-18-2012, 02:33 AM, (This post was last modified: 01-18-2012, 02:33 AM by Serial Thrilla.)
#8
RE: The Snippets Thread
Some *nix / Linux batch script for HLDS Valve gaming servers.


Half-Life Dedicated Server (HLDS):
Code:
#!/bin/bash

# Starting
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
while true; do
./hlds_i686 -console -game x -ip x -port x +maxplayers x +map x -pingboost 3
done

Replace the "x" with your value and add other parameters if need.
_______________________________________________________

Source Dedicated Server (SRCDS):
Code:
#!/bin/bash

# Starting
while true; do
./srcds_run -console -game x -ip x -port x +maxplayers x +map x
done

Replace the "x" with your value and add other parameters if need.
_______________________________________________________

Create a empty file and paste this code, modify it, so it looks how you need it and save the file into the root folder of your server (where srcds_run or hlds_i686 is).

Source: http://forum.singularity.us.to/thread-96.html (Yes, I'm the person called Technician)
Reply
04-12-2012, 07:28 AM, (This post was last modified: 04-12-2012, 07:29 AM by Arthur.)
#9
RE: The Snippets Thread
PHP function to add a JavaScript alert ($id allows you to give it an identifier so you know which it is [perhaps a later version will allow auto-numbering]):
PHP Code:
function alert_flag($id=false){
    if (
$id){
        
$flag ' Flag: '.$id;
    }
    echo 
'<script type="text/javascript">alert("Is this active?'.$flag.'");</script>';

Reply


Forum Jump: