PHP isInteger

function isInteger($n) {
  if (preg_match("/[^0-^9]+/",$n) > 0) {
    return false;
  } return true;
}

PHP 切换可见性

<script>
var status=1;
function changeObjectVisibility(objectId, newVisibility) {
    // first get a reference to the cross-browser style object 
    // and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
}

function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

function toggle() {

	if (status==1) {
		status=0;		
		changeObjectVisibility('container','visible');
	} else {
		status = 1;
		changeObjectVisibility('container','hidden');
	}
}
</script>

PHP strMiddleReduceWordSensitive

function strMiddleReduceWordSensitive ($string, $max = 50, $rep = '[...]') {
   $strlen = strlen($string);

   if ($strlen <= $max)
       return $string;

   $lengthtokeep = $max - strlen($rep);
   $start = 0;
   $end = 0;

   if (($lengthtokeep % 2) == 0) {
       $start = $lengthtokeep / 2;
       $end = $start;
   } else {
       $start = intval($lengthtokeep / 2);
       $end = $start + 1;
   }

   $i = $start;
   $tmp_string = $string;
   while ($i < $strlen) {
       if ($tmp_string[$i] == ' ') {
           $tmp_string = substr($tmp_string, 0, $i) . $rep;
           $return = $tmp_string;
       }
       $i++;
   }

   $i = $end;
   $tmp_string = strrev ($string);
   while ($i < $strlen) {
       if ($tmp_string[$i] == ' ') {
           $tmp_string = substr($tmp_string, 0, $i);
           $return .= strrev ($tmp_string);
       }
       $i++;
   }
   return $return;
   return substr($string, 0, $start) . $rep . substr($string, - $end);
}

PHP Reddit for WordPress插件

<?

/*
Plugin Name: Reddit
Plugin URI: http://www.seoegghead.com/
Description: Creates an interactive Reddit icon.
Author: Jaimie Sirovich
Version: 1.0
Author URI: http://www.seoegghead.com/
*/ 

function reddit_this($reddit_style = 3)
{
    global $id;
    $ref = $_SERVER['HTTP_REFERER'];
    $reddit_link = get_post_meta($id, 'REDDIT_CLASS_reddit_link', true);
    
    if ($reddit_link == '' && preg_match('#reddit.com#i', $ref)) { 
        add_post_meta($id, 'REDDIT_CLASS_reddit_link', $ref);
        $reddit_link = $ref;
    }

    if ($reddit_link) {
        ?><script>reddit_url='<?php the_permalink() ?>'</script><script language="javascript" src="http://reddit.com/button.js?t=<?=$reddit_style?>"></script><?
    }
    
}

?>

PHP 删除文件复选框中的行

#Datenverarbeitung
if (isset($HTTP_POST_VARS['delete'])) {
$inhalt = file('_matrix2.php');
$fp = fopen('_matrix2.php','w');
flock($fp,2);
while (list($inhalt_key,$inhalt_val) = each($inhalt)) {
if (!isset($HTTP_POST_VARS['delete'.$inhalt_key])) {
fwrite($fp,$inhalt_val);
}
}
flock($fp,3);
fclose($fp);
}
#ENDE: Datenverarbeitung

#Datenausgabe
$inhalt = file('_matrix2.php');

echo '<form method="post" action="'.$HTTP_SERVER_VARS['PHP_SELF'].'">';
while (list($inhalt_key,$inhalt_val) = each($inhalt)) {
echo $inhalt_val.'<input type="checkbox" name="delete'.$inhalt_key.'"><br>';
}
echo '<input type="submit" name="delete"></form>';
#ENDE: Datenausgabe

PHP 基本的计数器

<?php 
//tutorial for text-file based counter
$counthandle=fopen("visits2.txt","r");
//opens file for reading and writing
$getcurrent=fread($counthandle,filesize("visits2.txt"));
//reads in current value
$getcurrent=$getcurrent+1;
//increment count by 1
fclose($counthandle);
$counthandle1=fopen("visits2.txt","w");
fputs($counthandle1,$getcurrent);
//input new value
fclose($counthandle1);
$counthandle2=fopen("visits2.txt","r");
$getrecent=fread($counthandle2,filesize("visits2.txt"));
//gets new value
//print "$getrecent";
//print counter value
fclose($counthandle2);
?>

PHP 电子邮件编码器,以减少垃圾邮件

/**
 * Description: E-Mail encoder to reduce spam.
 *
 * @author Micke Johansson
 *
 * $mail. The e-mail address to encode.
 * $isLink. Set to true to create a link.
 * $display. What will be displayed in the browser. If omitted it will display the e-mail address.
 *
 * @param string $mail
 * @param bool $isLink
 * @param string $display
 * @return string Encoded e-mail or e-mail link
 */
function EncodeMail($mail, $isLink = false, $display = '')
{
	$domain = substr($mail,strpos($mail, '@')+1);
	$name = substr($mail,0, strpos($mail, '@'));
	$encodedDomain = '';
	$encodedName = '';
	$encodedDisplay = '';
	
	for ($i=0; $i < strlen($domain); $i++)
	{
		$encodedDomain .= '&#'.ord(substr($domain,$i)).';';
	}
	for ($i=0; $i < strlen($name); $i++)
	{
		$encodedName .= '&#'.ord(substr($name,$i)).';';
	}
	for ($i=0; $i < strlen($display); $i++)
	{
		$encodedDisplay .= '&#'.ord(substr($display,$i)).';';
	}
	$script = "<script type=\"text/javascript\">";
	$script .= "d=\"".$encodedDomain."\";";
	$script .= "n=\"".$encodedName."\";";
	if ($isLink)
	{
		if ($display == '')
			$script .= "document.write('<a href=\"mailto:'+n+'@'+d+'\">'+n+'@'+d+'</a>');";
		else
			$script .= "document.write('<a href=\"mailto:'+n+'@'+d+'\">".$encodedDisplay."</a>');";
	}
	else 
	{
		$script .= "document.write(n+'@'+d);";
	}
	$script .= "</script>";
	return $script;
}

PHP php mysql / mysqli查询

function sqlQuery( $sql )
{
  if( in_array( 'mysql', $GLOBALS[ 'loaded_extensions' ] ) )
  {
    $query = mysql_query( $sql ) or die(
      "<pre><font color=\"red\"><b>Invalid SQL:</b>\n\n("
      . mysql_errno()
      . ' ) '
      . mysql_error()
      . "\n\n"
      . "<b>Query:</b>\n"
      . eregi_replace( "\t", ' ', $sql )
      . '</font></pre>' );
  }
  elseif( in_array( 'mysqli', $GLOBALS[ 'loaded_extensions' ] ) )
  {
    $query = mysqli_query( $GLOBALS[ 'dbHandle' ], $sql ) or die(
      "<pre><font color=\"red\"><b>Invalid SQL:</b>\n\n("
      . mysqli_errno( $GLOBALS[ 'dbHandle' ] )
      . ' ) '
      . mysqli_error( $GLOBALS[ 'dbHandle' ] )
      . "\n\n"
      . "<b>Query:</b>\n"
      . eregi_replace( "\t", ' ', $sql )
      . '</font></pre>' );
  }

  return $query
}

PHP php mysql / mysqli num rows

function sqlNumRows( $query )
{
  if( in_array( 'mysql', $GLOBALS[ 'loaded_extensions' ] ) )
  {
    return mysql_num_rows( $query );
  }
  elseif( in_array( 'mysqli', $GLOBALS[ 'loaded_extensions' ] ) )
  {
    return mysqli_num_rows( $query );
  }
}

PHP php mysql / mysqli获取数组

function sqlFetchArray( $query, $type='MYSQL_ASSOC' )
{
  if( in_array( 'mysql', $GLOBALS[ 'loaded_extensions' ] ) )
  {
    switch( $type )
    {
      case 'MYSQL_NUM':
        return @mysql_fetch_array( $query, MYSQL_NUM );
        break;
      case 'MYSQL_ASSOC':
        return @mysql_fetch_array( $query, MYSQL_ASSOC );
        break;
      case 'MYSQL_BOTH':
        return @mysql_fetch_array( $query, MYSQL_BOTH );
        break;
    }
  }
  elseif( in_array( 'mysqli', $GLOBALS[ 'loaded_extensions' ] ) )
  {
    switch( $type )
    {
      case 'MYSQL_NUM':
        return @mysqli_fetch_array( $query, MYSQLI_NUM );
        break;    
      case 'MYSQL_ASSOC':
        return @mysqli_fetch_array( $query, MYSQLI_ASSOC );
        break;    
      case 'MYSQL_BOTH':
        return @mysqli_fetch_array( $query, MYSQLI_BOTH );
        break;
    }
  }
}