CoderZone.org
Pages: 1 « previous     next »
  Print  
Author Topic: Got some handy snippets?  (Read 5839 times) Bookmark and Share
Max
Jr. Member
*****
Posts: 75



View Profile WWW
« on: Jan 19, 2011, 03:23:38 pm »

If you have some cool, useful, or just plain handy snippets of code, we'd love to see 'em posted here. If they do something even slightly useful we'll add them to the code library with your name or nickname in the credit line.

Here's one I came across today- a simple, efficient way to find the first non-numeric character in a string...I was looking for a way to do that and found this slick one-liner:

Code:  
Highlight Mode: (PHP)
  1. preg_match("/\D/is", $myString, $match_list, PREG_OFFSET_CAPTURE);
  2.  
  3. $char_location = $match_list[0][1];
 

So, there's mine for the day. Have you all got something you'd like to share or show?
Logged
UnrealEd
Newbie
*
Posts: 22



View Profile
« Reply #1 on: Jan 19, 2011, 06:02:41 pm »

Here's a JavaScript snippet I came up with a couple of months ago...

Code:  
Highlight Mode: (Javascript)
  1. function js_root (filename) {
  2. var scripts = document.documentElement.getElementsByTagName ('script');
  3. for (var i=0; i<scripts.length; i++)
  4. if (scripts[i].src && scripts[i].src.substr (scripts[i].src.length - filename.length) == filename)
  5. return scripts[i].src.substr (0, scripts[i].src.length - filename.length);
  6. return window.location.href.substr(0, window.location.href.length - window.location.pathname.length + 1)
  7. }
 

It's part of a small JS framework I was working on (but never got around to finish it). What it does is find the location of a javascript file on the server when it's included via html. For example:
Code:  
Highlight Mode: (HTML)
  1. <script src="http://mysite.com/js/whatever/foo/bar/myfile.js" type="application/javascript"></script>
  2. <script type="application/javascript">
  3. // <![CDATA[
  4. alert(js_root('myfile.js');
  5. // ]]>
 
Would return http://mysite.com/js/whatever/foo/bar


It sounds stupid because you already know the location of the file, but it does make sense when you use it in a reusable library. That way you can place the JS file anywere on your server, allow the JS file itself to find were it's located and eventually load other js files relative to that path. Sounds all a bit redundant, but it did save me some ugly hard-coded settings  Tongue
Logged
wheels
Jr. Member
*****
Posts: 62



View Profile
« Reply #2 on: Jan 19, 2011, 08:19:38 pm »

I can't remember where I got this - but I really like it ...

Code:  
Highlight Mode: (Javascript)
  1. /* This allows dynamic includes of files */
  2. function include(filename)
  3. {
  4.        var head = document.getElementsByTagName('head')[0];
  5.  
  6.        script = document.createElement('script');
  7.        script.src = filename;
  8.        script.type = 'text/javascript';
  9.  
  10.        head.appendChild(script)
  11. }
  12.  
 

To use it ...

Code:  
Highlight Mode: (Javascript)
  1. include('js/more.js');
  2.  
 

And - if you want to allow only a single download of a file, you can put this in a file included into httpd.conf ...

Code:  
ExtFilterDefine session_id_check mode=output cmd="/opt/bin/webfilter.sh"

<Directory /var/www/html/downloads>
SetOutputFilter session_id_check
</Directory>

Code:  
Highlight Mode: (Bash)
  1. #!/bin/bash
  2. FILENAME="$DOCUMENT_URI";
  3. cat -
  4. if [ -e $FILENAME ]; then
  5.                echo "-" > $FILENAME;
  6. fi
  7.  
 

cat delivers the file, -e tests if it exists, and in this case, it echoes a dash, but you can use an rm if you prefer.

Be careful with this ... Smiley


« Last Edit: Jan 19, 2011, 08:27:33 pm by wheels » Logged
Max
Jr. Member
*****
Posts: 75



View Profile WWW
« Reply #3 on: Jan 19, 2011, 08:25:41 pm »

Here's a JavaScript snippet I came up with a couple of months ago...

That's pretty clever...I'm thinking that might work for something I have in mind to do later this year. Gotta bookmark this. Smiley

Add to the code library with your nick on it:
http://coderzone.org/library/Automatically-find-location-of-javascript-file_1075.htm
« Last Edit: Jan 19, 2011, 08:33:34 pm by Max » Logged
Max
Jr. Member
*****
Posts: 75



View Profile WWW
« Reply #4 on: Jan 19, 2011, 08:35:51 pm »

I can't remember where I got this - but I really like it ...

Cool stuff. Smiley
I'm trying to figure out how to add this to the code library so it all hangs together. Unfortunately the library only allows one setting for highlighting per snippet...let me see what I can figure out.
Logged
Max
Jr. Member
*****
Posts: 75



View Profile WWW
« Reply #5 on: Jan 19, 2011, 08:50:32 pm »

A (theoretically) handy php one-liner to create a random hex color:

Code:  
Highlight Mode: (PHP)
  1. $hexcolor = dechex(rand(0,255)) . dechex(rand(0,255)) . dechex(rand(0,255));
 

Logged
Tags: snippet 
Pages: 1
  Print  
 
Jump to: