NameSilo

Dan's snippets

Spaceship
Watch
Impact
133
Here's some of the more recent snippets/code I've written (PHP).

Create a multiplication table for number $num repeated $max times
This script will echo: (e.g) 2 x 1 = 2. The amount of times specified in
$max, using the number $num, in this example $num would equal 2.
PHP Version: Any
Link: http://danltn.com/bin/kbl.phps

==========================

Take screenshots with PHP
This class allows you to easily take screenshots on Windows assuming you
have the GD Lib and Internet Explorer installed, you can manipulate the
browser in a huge variety of ways and also use the additional Manipulation
class to edit your screenshot afterwards.
PHP Version: 5.2.2+
Link: http://www.phpclasses.org/browse/package/4608.html

==========================

Show the X most recent files in a folder
This script allows you to specify a folder and an amount and will return
that many files in reverse chronological order (most recent first). You
could then build a contents or a "most recent" list depending on what you
want. You can easily change the glob parameters to search for everything
('*') or add extra file types between the braces.
PHP Version: 4.3+
Link: http://danltn.com/bin/nwc.phps

==========================

Crawl Google pages for URLs
This script allows you to specify a search term in Google, and also how
many pages you want to crawl. Upon calling the function it will go through
the specified amount of Google pages and saving all the external (away from
Google) URLs it finds, it will then return them in a numerical array.
PHP Version: 4.3+
Link: http://danltn.com/bin/2da.phps

==========================

Debugging technique
I'm sure you've all used print_r to debug your scripts from time to time,
well I'd just like to point your attention to var_dump(). This not only
dumps all the values of the array (or object, or whatever) you're
debugging, but also whether it's a string, an int, a float, etc.

Unbelievably useful, and easy to use!
PHP Version: Any
Link: http://danltn.com/bin/4a8.phps

==========================

Don't put quotations around constants
Recently I've seen a lot of people put quotation marks around constants
that they actually wanted to be constants (not going into arrays where it's
often the other way around.)

If you click the download link you can clearly see why you should ensure
you
PHP Version: Any
Link: http://danltn.com/bin/a0p.phps

==========================

Easier solution for PHP Mail
Recently I've seen a lot of people, especially on forums struggling with
PHP mail. This is the class which other posts will be based off.
PHP Version: 5+
Link: http://danltn.com/bin/o0q.phps

==========================

PHP Mail - Example usage
In the previous example, I provided you with a PHP class to use. Here's the
link to the page if you need it. The class is quite long, confusing and
uses some more advanced techniques. If you click the nice large download
button below you can see an example of how it could be used (with HTML mail
on.)
PHP Version: 5+
Link: http://danltn.com/bin/0l5.phps

==========================

Avoid repeating str_replace 10+ times
Some people have been using str_replace upto 20 times in some more extreme
cases recently, when they just as easily could've used the array
functionality of str_replace. To help you understand how to do this the
download for this item is a short script which shows this functionality.
PHP Version: Any
Link: http://danltn.com/bin/p73.phps

==========================

AllTheWeb scraper
Recently someone needed an AllTheWeb scraper, but their code was fairly
buggy and hard to use, the download link this time is a fixed up version,
in the foreach loop (after we run extract) $name becomes the name of each
link, and $url the URL - You really couldn't have it easier than this!
PHP Version: 4.3+
Link: http://danltn.com/bin/zke.phps

==========================

Adjust current time for different timezones
This is a script which will take the current UNIX time (although
theoretically you could use any time) and then modify it so the returned
timestamp is changed +/- $adjust hours.

Pretty easy to pick up, just try it.
PHP Version: 5.1+
Link: http://danltn.com/bin/gz8.phps

==========================

Change memory limit
I've been surprised just how many people are unsure how to change the
memory limit on their PHP script, this will only work if ini_set hasn't
been disabled (it normally is enabled though, so no worries.)

Just one line is all you need!
PHP Version: Any
Link: http://danltn.com/bin/oo3.phps

==========================

Display a random image
This script allows you to set an array of URLs (4 have been set in this
demostration.)
The script will then choose a URL at random, read it into a string, echo
it and send the correct headers so the browser doesn't cache it either!
PHP Version: 4.3+
Link: http://danltn.com/bin/b6b.phps

==========================

Parse a valid RSS feed
You wouldn't believe how easy the RSS standards have made it to parse
almost any feed onto your site, make a title linked to the original page
and then even a paragraph below it so the user knows what the article is on
about before he clicks it.

This script requires the SimpleXML extension to parse the RSS feed, and in
this example will choose a random feed from an array, and show the 10 most
recent articles.
PHP Version: 5.1+
Link: http://danltn.com/bin/o6i.phps

==========================

How to check if headers have been sent.
A lot of people are often complaining that they receive the error: Headers
already sent when trying to forward a user to another page (or any other
use of the header function.) Thankfully within PHP exists another function
to check whether headers have been sent, you can even set parameters to the
function (documented at PHP.net) so that it will tell you where the headers
were sent (normally at first output.)

Even better, it's practically a one line script, so it's definitely worth
implementing to avoid problems.
PHP Version: Any
Link: http://danltn.com/bin/57q.phps

==========================

Simplify multiple function_exist calls into one function
This function allows you to easily call multiple function checks in a
single function, and it will only return true if all the functions exist -
almost definitely what you need. Now you can easily check whether those
functions your script needs exist on the server
PHP Version: Any
Link: http://danltn.com/bin/gxz.phps

==========================

Check if today is Christmas Day
This function is extremely simple but fun nonetheless, all it does in
simple terms is tell you whether today (or the date you specified) is
Christmas day, if it is then it returns (boolean) true, otherwise it
returns (boolean) false, a simple usage of it would be if(is_christmas()) {
... }

Not made for anything serious just have fun with it.
PHP Version: 4.3+
Link: http://danltn.com/bin/l7f.phps

==========================

Remove slashes from GET and POST
A lot of people often complain about the fact that their GET and POST
arrays often return data which has backslashes infront of characters such
as ' and ".

This is because of a "feature" (notice the quotations) called magic
quotes.

The download link shows a small snippet on how to remove these for
none-multidimensional arrays (most aren't).
PHP Version: Any
Link: http://danltn.com/bin/5yr.phps

==========================

Remove slashes from multidimensional GET and POST
This is the same as the last post (see that for details.) With the
exception that this will work for multidimensional arrays, you probably
won't need it for most sites, but some might... So here it is.
PHP Version: Any
Link: http://danltn.com/bin/gen.phps

==========================

If I've accidentally used your code in any of those without attribution (I use a lot of snippets I find around and I may not have correct attributed you.) If this is the case (albeit unlikely) - Please contact me and I'll remove/fix it as necessary.

Enjoy them,

Dan.
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Good snippets. :tu:

I notice in the RSS Feeds snippet you have used the file_get_contents function on a remote file. Some shared hosts have this disabled and recommend to use cURL instead.
 
0
•••
2 x 1 = 2 (not 1) so you might want to correct that in the description. However, 1^2 is 1.
 
0
•••
0
•••
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back