Articles Tagged 'Snippets'


Very short snippet: PHP plain dates

. Converts a date in the format yyyy-mm-dd hh:mm:ss to yyyymmddhhmmss . Useful to exploit the functions of ordering, the type asort() .

1
2
3
4
5
6
/ **
* Format from "yyyy-mm-dd hh: mm: ss" to "yyyymmddhhmmss"
* /
$d ) { plainDate function ($ d) {
preg_replace ( '/(-|:|\040)/' , '' , $d ) ) ; return ( preg_replace ('/ (- |: | \ 040) /','', $ d));
}

More ...

Variety of coding and coding

Some argue that programming is an art and, ultimately, I can only agree, especially when you unearth very different solutions for the same problem. To understand how true this is how an identical need in medisimo language (JavaScript) can be solved with approaches very different and original.

Left zero pad

. A number, but the discussion is also valid for any string, such as 123 can be filled to the left - to aerate - with a certain number of zeros, for example: 00123 . This need has on several occasions and is useful to put in a column - or at least show - a number in a clean way, implicitly indicating its maximum value. . In video games, for example, the classical score (the score) is often referred to 001234 , indicating that at most we will arrive at 999999 . Here's how the same problem has been solved by various developers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/ **
* Left Pad String
*
* @ From http://snipplr.com/view/8423/left-pad-string/
* @ Author web-http://www.mechanicmatt.com/
*
* @ Param num - Striga to fill
* @ Param totalChars - Total number of characters, including the "zeros"
* @ Param padWidth - Character used to fill, default "0"
* /
num , totalChars , padWith ) { leadingZeros function (num, totalChars, padWith) {
"" ; num = num + "";
padWith ) ? padWith : "0" ; padWith = (padWith)? padWith: "0";
num. length < totalChars ) { if (no. length <totalChars) {
num. length < totalChars ) { while (no. length <totalChars) {
num ; num = num + padWith;
}
{ } Else {}}

num. length > totalChars ) { //if padWith was a multiple character string and num was overpadded if (no. length> totalChars) {/ / if padWith was multiple character string and num was overpadded
( ( num. length - totalChars ) , totalChars ) ; num = num. substring ((no. length - totalChars), totalChars);
{ } Else {}}

return num;
}
leadingZeros ( "asdf" , 10 , "0" ) ) ; alert (leadingZeros ("asdf", 10, "0"));

scegliendo anche il tipo di carattere da usare tramite padWidth , invece del default 0 . This solution is extremely articulate, yet allows you to add any number of 0 to the number num choosing the type of font used by padWidth , instead of the default 0 .
Decidedly original, however, this solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
/ **
* String_pad
*
* @ From http://snipplr.com/view/700/stringpad/
* @ Author http://d.hatena.ne.jp/brazil/20060721/1153489937
*
* @ Param str - Striga to fill
* @ Param len - Number of characters, including the "zeros"
* @ Param ch - character used to fill
* /
str , len , ch ) { return new Array ( len - ( '' + str ) . length + 1 ) . join ( ch ) + str } function pad (str, len, ch) {return new Array (len - (str +''). length + 1). join (ch) + str}

pad ( 56 , 4 , '0' ) ) ; // 0056 alert (pad (56, 4, '0 ')); / / 0056

Same result, with a completely different approach. . Again we have the opportunity to decide the number of padding characters using parameter len , and the font to use with ch . Missing is the use of default provided in the above function.
The one I use, but ...:

1
2
3
4
5
6
7
8
9
10
11
12
/ **
* String_pad
*
* @ Author Giovambattista Fazioli
* @ Web http://www.undolog.com
*
* @ Param s - Striga to fill
* @ Param s - the character string that indicates either that the length
* Eg "0000" = "0" character length 4
* /
s , l ) { return ( l. substr ( 0 , ( l. length - s. length ) ) + s ) ; } padding function (s, l) {return (l. substr (0, (l. length - s. length)) + s);}
padding ( '123' , '0000' ) ) ; alert (padding ('123 ', '0000'));

If you have other interesting solutions do not hesitate to comment : D

More ...

Very short snippet: PHP word cut

Delimits a portion of text to the number of "words":

1
2
3
4
5
6
7
8
9
10
11
/ **
* String word cut
*
* @ Private
* /
$c , $l ) { wordCut function ($ c, $ l) {
explode ( ' ' , $c ) ; $ C = explode ('', $ c);
$i = 0 ; $i < $l ; $i ++ ) $r [ $i ] = $c [ $i ] ; for ($ i = 0; $ i <$ l $ i + +) $ r [$ i] = $ c [$ i];
implode ( ' ' , $r ) . '...' ; $ R = implode ('', $ r). '...';
; return $ r;
}

More ...

Extending the Flash CS3 IDE with Snippets

Snippets Lee Brimelow , Platform Evangelist at Adobe, is concerned with developing in Flash, Flex and AIR. From his blog ( TheFlashBlog ) you can download a handy extension for Flash CS3: Snippets . This extension is written using the API that Flash provides to interact with its IDE (JSFL API) allows you to add a touch panel (along the lines of that already present in Adobe Dreamweaver) where you can insert small pieces of code that we use most often.

Once installed you can already taken advantage of some snippets provided by the author. Clicking on the icon image You can edit the XML file that contains snippets (the pieces of code). Once you make the change simply save the file and click on Update snippets to update the list. On the author's site have been released in recent days more snippets to add to those already present. Here is the full version of the XML file:

More ...



Stop SOPA