PHP ucname / Title Case

function ucname($string) {
	$string = ucwords(strtolower($string));

	foreach(array('-', '\'') as $delimiter) {
		if (strpos($string, $delimiter) !== false) {
			$string = implode($delimiter, array_map('ucfirst', explode($delimiter, $string)));
		}
	}
	
	return $string;
}

PHP 冒泡排序整数

/*
  sortIntegers()
  ---
  Uses the "bubble sort" algorithm to sort an array of
  integers. PHP's sorting functions are fine, so this
  function is merely to demonstrate how it could be done.
*/

function sortIntegers($x)
{
  $count = count($x)-1;
  
  for ($i=0; $i<$count; $i++)
  {
    if ($x[$i]<$x[$i+1]) continue;
    
    list($x[$i],$x[$i+1]) = array($x[$i+1],$x[$i]);
    
    $x = sortIntegers($x);
  }
  return $x;
}

// Example

$integers = array(6,3,2,8,9,4,0,1,7,5);

$sorted = sortIntegers($integers);

echo '<pre>', implode(',',$sorted), '</pre>';

PHP rgb2hex

function rgb2hex($r, $g, $b, $uppercase=false, $shorten=false)
{
  // The output
  $out = "";
  
  // If shorten should be attempted, determine if it is even possible
  if ($shorten && ($r + $g + $b) % 17 !== 0) $shorten = false;
  
  // Red, green and blue as color
  foreach (array($r, $g, $b) as $c)
  {
    // The HEX equivalent
    $hex = base_convert($c, 10, 16);
    
    // If it should be shortened, and if it is possible, then
    // only grab the first HEX character
    if ($shorten) $out .= $hex[0];
    
    // Otherwise add the full HEX value (if the decimal color
    // is below 16 then we have to prepend a 0 to it)
    else $out .= ($c < 16) ? ("0".$hex) : $hex;
  }
  // Package and away we go!
  return $uppercase ? strtoupper($out) : $out;
}

echo "#", rgb2hex(250, 123, 0, true), // Gives: #FA7B00
 "<br>#", rgb2hex(170, 0, 255), // Gives: #aa00ff
 "<br>#", rgb2hex(170, 0, 255, false, true); // Gives: #a0f

PHP hex2rgb

if ( ! function_exists("hex2rgb"))
{
  function hex2rgb($h, $a=false)
  {
    if ($h[0] == '#') $h = substr($h,1);
    
    if (strlen($h) == 3)
    {
      $h = str_split($h,1);
      
      foreach ($h as &$c) $c .= $c;
    }
    elseif (strlen($h) == 6)
    {
      $h = str_split($h,2);
    }
    else return null;
    
    foreach ($h as &$t)
    {
      $t = base_convert($t,16,10);
    }
    return $a ? array_combine(array('red','green','blue'),$h)
    
    : implode(',',$h);
  }
}

PHP 在Wordpress 3.0中添加页面摘录

add_post_type_support( 'page', 'excerpt' );

HTML 将Flash嵌入HTML

<object width="940" height="165">
<param name="movie" value="your.swf">
<param name="wmode" value="transparent">
<embed src="your.swf" width="940" height="165" wmode="transparent">
</embed>
</object>

JavaScript JavaScript重定向

<script type="text/javascript">
	window.location.href = 'http://www.google.com/';
</script>

PHP 用空格替换空格

preg_replace('/\s\s+/', ' ', $str);

PHP PHP:readfile - 手动

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

jQuery Superfish - 在子菜单中单击仍保持打开状态

$(function(){
    var menu = $("#nav");

    menu.find("ul.sf-menu")
        .superfish({
            delay:         0,
            speed:         'fast',
            autoArrows:    false,
            dropShadows:   false,
            onHide:        function(){
                if (this.parent().is('.sfPersist')) {
                    this.show().css('visibility','visible').parent().addClass('sfHover');
                }
            }
        })
        .find('li > ul > li').click(function(){
            // hide previously persistent children (LOL that just sounds wrong)
            menu.find('.sfPersist')
                .removeClass('sfPersist sfHover')
                .find('> ul').hide().css('visibility','hidden');

            // add children that should be persistent
            if ($(this).is('.sfSelected')) {
                // if previously selected, keep hidden
                menu.find('li.sfSelected').removeClass('sfSelected');
            } else {
                // Hide other selected classes
                menu.find('li.sfSelected').removeClass('sfSelected');
                // if newly selected, then show
                $(this)
                    .addClass('sfSelected') // remember which one is selected
                    .parent()
                    .show().css('visibility','visible')
                    .parent().addClass('sfHover sfPersist');
            }
        });
});