The Snippets Thread - Printable Version +- NiftyHost Forums (Archive) (https://niftyhost.chary.us/support) +-- Forum: Technology (https://niftyhost.chary.us/support/forum-7.html) +--- Forum: Coding (https://niftyhost.chary.us/support/forum-12.html) +--- Thread: The Snippets Thread (/thread-1339.html) |
The Snippets Thread - MyDigitalpoint - 08-15-2011 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 RE: The Snippets Thread - HiddenKnowledge - 10-02-2011 This snippet is supposed to connect you to a mysql server and database using the basic mysql functions. PHP Code: <?php RE: The Snippets Thread - MyDigitalpoint - 10-20-2011 This snippet generates random hexadecimal number to get HTML colors PHP Code: <?php 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; ?>;"> RE: The Snippets Thread - Zach - 10-20-2011 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) RE: The Snippets Thread - Arthur - 10-27-2011 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){ 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){ RE: The Snippets Thread - Zach - 10-27-2011 @Arthur: You might have some trouble with your first function. empty() is a pre-defined function in PHP. RE: The Snippets Thread - Arthur - 10-27-2011 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! RE: The Snippets Thread - Serial Thrilla - 01-18-2012 Some *nix / Linux batch script for HLDS Valve gaming servers. Half-Life Dedicated Server (HLDS): Code: #!/bin/bash Replace the "x" with your value and add other parameters if need. _______________________________________________________ Source Dedicated Server (SRCDS): Code: #!/bin/bash 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) RE: The Snippets Thread - Arthur - 04-12-2012 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){ |