Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Remove magic quotes on GPC data

Removes magic quotes on $_GET, $_POST, $_COOKIE, $_FILES and $_REQUEST, when they are enabled.

<?php

// remove magic_quotes
if(get_magic_quotes_gpc())
{
  function undo_magic_quotes_array($array)
  {
    return is_array($array) ? array_map('undo_magic_quotes_array', $array) : str_replace("\\'", "'",
							                                                               str_replace("\\\"", "\"",
                                                                             str_replace("\\\\", "\\",
                                                                             str_replace("\\\x00", "\x00", $array))));
  }

  $_GET = undo_magic_quotes_array($_GET);
  $_POST = undo_magic_quotes_array($_POST);
  $_COOKIE = undo_magic_quotes_array($_COOKIE);
  $_FILES = undo_magic_quotes_array($_FILES);
  $_REQUEST = undo_magic_quotes_array($_REQUEST);
}

?>

Remove magic quotes on GPC data

Removes magic quotes on $_GET, $_POST, $_COOKIE, $_FILES and $_REQUEST, when they are enabled.

<?php

// remove magic_quotes
if(get_magic_quotes_gpc())
{
  function undo_magic_quotes_array($array)
  {
    return is_array($array) ? array_map('undo_magic_quotes_array', $array) : str_replace("\\'", "'",
							                                                               str_replace("\\\"", "\"",
                                                                             str_replace("\\\\", "\\",
                                                                             str_replace("\\\x00", "\x00", $array))));
  }

  $_GET = undo_magic_quotes_array($_GET);
  $_POST = undo_magic_quotes_array($_POST);
  $_COOKIE = undo_magic_quotes_array($_COOKIE);
  $_FILES = undo_magic_quotes_array($_FILES);
  $_REQUEST = undo_magic_quotes_array($_REQUEST);
}

?>

magic square demo

REBOL [
    Title:  "Magic Square program"
    Author: "Gregg Irwin"
    Comment: {
        Credit to Bob Kurosaka and Martin Gardener for inspiration.
    }
]

get-size: has [result] [
    print "Enter the size of the square's side."
    print ""
    result: to-integer ask "It must be an odd number larger than 1 : "
    if any [(result < 3) (even? result)] [get-size]
    result
]

edge-guard: does [
    if row < 1    [row: row + side]
    if col > side [col: col - side]
    if row > side [row: row - side]
    if col < 1    [col: col + side]
]

print-square: func [square side] [
    print ["Magic Square, Order " side]
    print ["Each row, col, and Diagonal add up to " side * (side ** 2 + 1) / 2]
    print ""
    repeat row side [
        repeat col side [
            prin [pick square/:row col tab]
        ]
        print ""
    ]
    print ""
]


magic-square: func [/size sz [integer!]] [
    side: either size [sz][get-size]
    row: 1
    col: (side + 1) / 2         ;Locate the starting cell
    square: array/initial reduce [side side] 0
    poke square/:row col 1      ;Initialize the first value

    for i 2 to-integer (side ** 2) 1 [
        row: row - 1
        col: col + 1                  ;Northeast move
        edge-guard
        if 0 <> pick square/:row col [
            row: row + 1              ;otherwise retreat
            col: col - 1
            row: row + 1              ;Break move
            edge-guard
        ]
        poke square/:row col i
    ]
    print-square square side
]


;-- 0 based version --

; print-square: func [square side] [
;     print ["Magic Square, Order " side]
;     print ["Each row, col, and Diagonal add up to " side * (side ** 2 + 1) / 2  - side]
;     print ""
;     repeat row side [
;         repeat col side [
;             prin [pick square/:row col tab]
;         ]
;         print ""
;     ]
;     print ""
; ]
; 
; magic-square: func [/size sz [integer!]] [
;     side: either size [sz][get-size]
;     row: 1
;     col: (side + 1) / 2         ;Locate the starting cell
;     square: array/initial reduce [side side] none
;     poke square/:row col 0      ;Initialize the first value
; 
;     repeat i to-integer (side ** 2) - 1 [
;         row: row - 1
;         col: col + 1                  ;Northeast move
;         edge-guard
;         if none <> pick square/:row col [
;             row: row + 1              ;otherwise retreat
;             col: col - 1
;             row: row + 1              ;Break move
;             edge-guard
;         ]
;         poke square/:row col i
;     ]
;     print-square square side
; ]

;-- End 0 based version --

magic-square
;magic-square/size 5

halt

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS