IMPORTANT!

Snipt is going open source. We've toyed with this idea for quite a while, and have finally decided it's the right way to move forward.

A few things:
  • The entire Snipt source code will be released on GitHub under the 3-clause BSD License on Friday, September 10th.
  • While we'd like to think we're perfect, we realize we're only human. By open sourcing the software that runs this website, certain bugs or security flaws may be discovered that could compromise the privacy of your snipts.
  • Only the Lion Burger team will be able to push commits to the Snipt.net site. Contributors should send a pull request to add new features or submit patches.
  • By using this site, you agree not to be too angry or take any legal action against Lion Burger should this whole thing go up in flames some day.
  • Follow us on Twitter for updates.
I agree, close this message
Sign up to create your own snipts, or login.

Latest 100 public snipts » php The latest public php snipts.

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
    

    copy | embed

    0 comments - tagged in  posted by artilibere on Sep 03, 2010 at 8:19 a.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by artilibere on Sep 03, 2010 at 8:10 a.m. EDT
  • 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
    

    copy | embed

    0 comments - tagged in  posted by artilibere on Sep 03, 2010 at 8:08 a.m. EDT
  • php in html via .htaccess
    AddType application/x-httpd-php .php .htm .html
    

    copy | embed

    0 comments - tagged in  posted by dobersby on Sep 02, 2010 at 5:09 a.m. EDT
  • Get Template Directory
    <?php bloginfo( 'template_directory' ); ?>
    

    copy | embed

    0 comments - tagged in  posted by skotmxpx on Sep 01, 2010 at 4:46 p.m. EDT
  • php
    <?php
       echo 'test';
    ?>
    

    copy | embed

    0 comments - tagged in  posted by kungkk on Aug 31, 2010 at 9:13 p.m. EDT
  • 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
    
    ?>
    

    copy | embed

    0 comments - tagged in  posted by Rhinos on Aug 28, 2010 at 1:17 p.m. EDT
  • Prints out print_r in a readable format
    // PRINTS OUT PRINT_R REALLY PRETTY
    function print_r_html ($arr) {
            ?><pre><?
            print_r($arr);
            ?></pre><?
    }
    

    copy | embed

    0 comments - tagged in  posted by gregwhitworth on Aug 18, 2010 at 1:32 p.m. EDT
  • php get root dir
    $docRoot = getenv("DOCUMENT_ROOT");
    include $docRoot."/includes/config.php";
    

    copy | embed

    0 comments - tagged in  posted by aike on Aug 16, 2010 at 12:10 p.m. EDT
  • Getting rid of the 'allowed tags' in Wordpress comment forms.
    <?php comment_form(array('comment_notes_after' => '')); ?>
    

    copy | embed

    0 comments - tagged in  posted by d13t on Aug 16, 2010 at 6:05 a.m. EDT
  • 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');
    ?>
    

    copy | embed

    0 comments - tagged in  posted by depi on Aug 14, 2010 at 12:57 p.m. EDT
  • 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.... */
    ?>
    

    copy | embed

    0 comments - tagged in  posted by toannk on Aug 13, 2010 at 6:38 a.m. EDT
  • wordpress display custom value
    <?php $values = get_post_custom_values('image'); echo $values[0]; ?>
    

    copy | embed

    0 comments - tagged in  posted by aike on Aug 12, 2010 at 12:19 p.m. EDT
  • 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>
    

    copy | embed

    0 comments - tagged in  posted by d13t on Aug 10, 2010 at 3:40 a.m. EDT
  • 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; ?>
    

    copy | embed

    0 comments - tagged in  posted by aike on Aug 06, 2010 at 4:08 a.m. EDT
  • PHP timezone conversion in one line
    echo date_create($userDate)->setTimezone(new DateTimeZone($userTimezone))->format(‘Y-m-d H:i’);
    

    copy | embed

    0 comments - tagged in  posted by wayfarer on Aug 05, 2010 at 10:50 a.m. EDT
  • removes heading in list pages
    <ul>
    <?php wp_list_pages('title_li='); ?>
    </ul>
    

    copy | embed

    0 comments - tagged in  posted by aike on Aug 05, 2010 at 6:40 a.m. EDT
  • 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');
    
    ?>
    

    copy | embed

    0 comments - tagged in  posted by aike on Aug 05, 2010 at 6:38 a.m. EDT
  • 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];
    }
    

    copy | embed

    0 comments - tagged in  posted by jrguitar21 on Aug 04, 2010 at 9:38 p.m. EDT
  • 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;
    }
    ?>
    

    copy | embed

    0 comments - tagged in  posted by bradrozier on Aug 03, 2010 at 1:03 p.m. EDT
Sign up to create your own snipts, or login.