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-6 of 6 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);
}

?>

quote-delineated tag sets

snippet to surround a tag with “smart quotes” if it contains any odd characters. i.e. to display tags entered in a flickr-like style (space separated except inside quotes)

tag.gsub(/^/,'&ldquo;').gsub(/$/,'&rdquo;') if tag[0].match(/[^A-Za-z0-9\-_.]/)

Single and double quotes do differ in ruby

// description of your code here

my_string = 'This is mine!'
your_string = "This is yours!"
my_string += '\n\t\tAgain, I say!'
your_string += "\n\t\tAgain, I say!"
puts my_string
# => This is mine!\n\t\tAgain, I say!
puts your_string
# => This is yours!
# Again, I say!

r = 42
the_answer = "The answer is #{r}!"
# => "The answer is 42!"
the_answer = 'The answer is #{r}!'
# => "The answer is \#{r}!"


Random quotes

Random quotes

<?php
$quotes=array(
'Neo',
'Morpheus',
'Trinity',
'Cypher',
'Tank'
);
echo $quotes[rand(0,sizeof($quotes)-1)];
?>

Random quotes

Called as quotes.php it will display a single random quote. Called as quotes.php?all=1 it will display all quotes in an unordered list. No database is required, and it should be fairly obvious how to add new quotes.

<?php
$quotes[] = "Boys like hugs too.";
$quotes[] = "Ooh! Train!";
$quotes[] = "La rue est a nous.";
$quotes[] = "The future is unwritten.";

if($all==1) {
        echo "<ul>";
        for($i=0; $i<=count($quotes)-1; $i++) {
                echo "<li>".$quotes[$i]."</li>";
        }
        echo "</ul>";
} else {
        srand ((double) microtime() * 1000000);
        $randomquote = rand(0,count($quotes)-1);
        echo $quotes[$randomquote];
}

?>

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