Latest 100 public
snipts » php
showing 1-20 of 453 snipts for php
-
∞ Extract domain name from a full url
/** * Function to get the exact domain name * @example: given http://www.example.com/dir1/file.php , returns www.example.com /**/ function getDomain($url){ $domainName = explode("/",$url); if(isset($domainName[2])) return $domainName[2]; else return NULL; }#end getDomain
-
∞ A Friendly print_r version
/** * Friendly print_r version **/ function print_arr($variabile, $text='', $return=FALSE){ if($text<>'') $text=" $text="; $type=gettype($variabile); switch($type){ case "array": case "object": #$out="<p><pre>$text".var_export($variabile,TRUE)."</pre></p>"; $out="<p><pre>$text".print_r($variabile,TRUE)."</pre></p>"; break; case "NULL": $str="<p>NULL</p>"; case "string": default: if(!isset($str)) $str=strval($variabile); $out="<p><pre>#".$type."[".strlen($str)."]:$text".($str)."</pre></p>"; break; } if(!$return) echo $out; else return $out; }#end print_arr
-
∞ Generate a random numeric zero-padded string
/** * Function to generate a random numeric zero-padded string * @param int $minChar Minimum character of the numer * @param int $maxChar Maximum character of the numer **/ function getRandom($minChar=5, $maxChar=9){ $min=pow(10,$minChar-1); $max=pow(10,$maxChar)-1; list($usec, $sec) = explode(' ', microtime()); srand((float) $sec + ((float) $usec * 100000)); return str_pad(rand($min,$max),$maxChar,"0",STR_PAD_LEFT); }#end getRandom
-
∞ php in html via .htaccess
AddType application/x-httpd-php .php .htm .html
-
∞ Get Template Directory
<?php bloginfo( 'template_directory' ); ?>
-
∞ php
<?php echo 'test'; ?>
-
∞ Sort a multi-dimensional array by a certain column
<?php // The data (could be from a query result) $data = array( array( 'name' => 'John' 'age' => 37 ), array( 'name' => 'Lucy', 'age' => 32 ), array( 'name' => 'Ben', 'age' => 42 ) ); // sort a multi-dimensional array by a certain column $name = $age = array(); foreach ($data as $key => $value) { $name[$key] = $value['name']; $age[$key] = $value['age']; } // Sort the array by age in ascending order array_multisort($age, SORT_ASC, $data); // Sort the array by name in descending order array_multisort($name, SORT_DESC, $data); unset($name, $age); // remember to remove the variables you won't be using later on ?>
-
∞ Prints out print_r in a readable format
// PRINTS OUT PRINT_R REALLY PRETTY function print_r_html ($arr) { ?><pre><? print_r($arr); ?></pre><? }
-
∞ php get root dir
$docRoot = getenv("DOCUMENT_ROOT"); include $docRoot."/includes/config.php";
-
∞ Getting rid of the 'allowed tags' in Wordpress comment forms.
<?php comment_form(array('comment_notes_after' => '')); ?>
-
∞ Modify the_excerpt() output
<?php function my_excerpt_length($text){ return 10; } add_filter('excerpt_length', 'my_excerpt_length'); function new_excerpt_more($more) { return '...'; } add_filter('excerpt_more', 'new_excerpt_more'); ?>
-
∞ Detect an AJAX Request in PHP
<?php /* decide what the content should be up here .... */ $content = get_content(); //generic function; /* AJAX check */ if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { /* special ajax here */ die($content); } /* not ajax, do more.... */ ?>
-
∞ wordpress display custom value
<?php $values = get_post_custom_values('image'); echo $values[0]; ?>
-
∞ looping through a directory to get a linked list of files
// This: <ol> <? $dir = "files/*"; foreach(glob($dir) as $file) { ?> <li><p><a href='<?=$file?>'><?=basename($file)?></a></p></li> <? } ?> </ol> // Instead of this oldtimer: <ol> <? function getMap($dir){ if (is_dir($dir)){ $file = opendir($dir); while (true == ($map = readdir($file))){ if ($map!=".."&&$map!=".") $array[] = $map; } closedir($file); return $array; } } if (is_dir("files")){ foreach ((array) getMap("files") as $key => $value) { ?> <li><p><a href='files/<?=$value?>'><?=$value?></a></p></li> <? } } ?> </ol>
-
∞ worrdpress loop
/*THE LOOP - START*/ <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> /*THE LOOP - END*/ <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>
-
∞ PHP timezone conversion in one line
echo date_create($userDate)->setTimezone(new DateTimeZone($userTimezone))->format(‘Y-m-d H:i’); -
∞ removes heading in list pages
<ul> <?php wp_list_pages('title_li='); ?> </ul>
-
∞ adds first-page-item class to first item in list-pages
<?php function add_markup_pages($output) { $output= preg_replace('/page-item/', ' first-page-item page-item', $output, 1); $output=substr_replace($output, " last-page-item page-item", strripos($output, "page-item"), strlen("page-item")); return $output; } add_filter('wp_list_pages', 'add_markup_pages'); ?>
-
∞ drupal_get_machinename
<?php /** * Most machine names are generated as-you-type via Javascript helpers on the * client side. This helper function automates server-side conversions from * regular text strings into valid Drupal machine names. * * Machine names must contain only lowercase alpha-numerics and underscores. * Non-alphanumeric characters are converted to underscores, and sequential * underscore characters are trimmed to a single character. * * Additionally, if the transliteration module is enabled, then it will be * used to first convert non-ascii Unicode characters to standard Roman ASCII * characters on a best-effort basis, and replacing all unknown characters * with underscores. * * @param $text * A string of characters to be converted. * * @return * A string containing the valid Drupal machine name for the specified text. * Returns the empty string if invalid input was provided. * * @see * See the transliteration module in the drupal contrib repository for more * information on non-ascii character transliterations. */ function drupal_get_machinename($text) { // Cache machine name conversions locally for performance boost. static $texts; // If the input string is not valid, return an empty string. if (empty($text) || !is_string($text)) { return ''; } // Setup the local static variable cache if it doesnt exist. if (!is_array($texts)) { $texts = array(); } // Save the machine name conversion to the static cache if it doesnt exist. if (!isset($texts[$text])) { $out = $text; if (module_exists('transliteration')) { $out = transliteration_get($out, '_'); } $out = strtolower($out); $out = preg_replace('/[^a-z0-9]/', '_', $out); $out = preg_replace('/_+/', '_', $out); $texts[$text] = $out; } return $texts[$text]; }
-
∞ Escape data for SQL Server: mssql_real_escape_string
<? function mssql_real_escape_string($s) { if(get_magic_quotes_gpc()) { $s = stripslashes($s); } $s = str_replace("'","''",$s); return $s; } ?>


