admin on November 21st, 2009

Start with an array of words to censor <?php //The array of words to censor $censorwords = array(“word1″, “word2″, “word3″); ?> A simple example using fixed length replacement <?php function censor2($txt,$censorwords){ foreach($censorwords as $v){ $replace_arr[] = “/\b$v\b/i”; } return preg_replace($replace_arr, array(”****”), $txt); } ?> An example using string length replacement <?php function censor($txt,$censorwords){ foreach($censorwords as [...]

Continue reading about Censoring Text

admin on November 21st, 2009

To get the current id value connect to database and use the following query //get current id <?php $result = pg_exec($conn, “SELECT currval(‘table_id_seq’) AS id”); $id = pg_result($result, 0, “id”); //free result pg_freeresult($result); ?>

Continue reading about Latest Table ID

admin on November 21st, 2009

There are a couple ways to tell if a variable is even or odd <?php if ($i % 2) { echo “$i is odd”; } else { echo “$i is even”; } ?> OR <?php if ($i & 1) { echo “$i is odd”; } else { echo “$i is even”; } ?>

Continue reading about Even or Odd

admin on November 21st, 2009

This is a handy script that I found to create random password so you don”t have to be creative and think of them. <?php function generatePwd($chars=”7″) { //password $password = “”; //draw password from $salt = “abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789″; //random number generator srand((double)microtime()*1000000); for ($i = 0; $i < $chars; $i++) { $password .= substr($salt, rand() % [...]

Continue reading about Password Generator

admin on November 21st, 2009

To get the document root of the website <?php echo $_SERVER[''DOCUMENT_ROOT'']; ?>

Continue reading about Website Document Root

admin on November 21st, 2009

Check for a SSL connection <?php function check_ssl() { // Check if accessed via SSL if($_SERVER[''HTTPS''] != ”on”) { // If not, redirect $newurl = ”https://”.$_SERVER[''SERVER_NAME'']. $_SERVER[''REQUEST_URI'']; header(“location: $newurl”); } } ?>

Continue reading about Check for SSL

admin on November 21st, 2009

To get any number of random images use the following idea. <?php //defaults $path = “/path/to/image/directory/”; $extensions = array(“jpg”, “gif”, “png”); //open directory $dir = opendir($path); //loop through directory files while ($file = readdir($dir)) { //get extension $ext = substr($file, -3); $ext = strtolower($ext); //loop through extenstions array for ($i=0; $i < count($extensions); $i++) { [...]

Continue reading about Pull Random Images From Directory

admin on November 21st, 2009

To parse php in front page Allow htm pages to be parsed as php pages AddType application/x-httpd-php htm Turn on asp tags in Apache php_flag asp_tags On

Continue reading about Parse PHP in Front Page

admin on November 21st, 2009

Email Address: <?php $email = “someone@example.com”; if(eregi(“^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+ (\.[a-z0-9-]+)*(\.[a-z]{2,3})$”, $email)) { echo “Valid email address.”; } else { echo “Invalid email address.”; } ?> Just Numbers: <?php $var = “12345″; if (ereg(”[^0-9]”, $var)) { echo “This contains characters other than numbers”; } else { echo “This contains only numbers”; } ?> Numbers And “-”: <?php $var [...]

Continue reading about Validating Variables/Forms