PHP开发网站常用的21种功能的代码汇总(2)

http://www.itjxue.com  2015-07-17 08:18  来源:未知  点击次数: 

8. 解析 XML 数据

//xml string
$xml_string="<?xml version='1.0'?>
<users>
   <user id='398'>
      <name>Foo</name>
      <email>foo@bar.com</name>
   </user>
   <user id='867'>
      <name>Foobar</name>
      <email>foobar@foo.com</name>
   </user>
</users>"; 
//load the xml string using simplexml
$xml = simplexml_load_string($xml_string); 
//loop through the each node of user
foreach ($xml->user as $user)
{
   //access attribute
   echo $user['id'], '  ';
   //subnodes are accessed by -> operator
   echo $user->name, '  ';
   echo $user->email, '<br />';
}

9. 创建日志缩略名

创建用户友好的日志缩略名。

function create_slug($string){
    $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
    return $slug;
}

10. 获取客户端真实 IP 地址

该函数将获取用户的真实 IP 地址,即便他使用代理服务器

function getRealIpAddr()
{
    if (!emptyempty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!emptyempty($_SERVER['HTTP_X_FORWARDED_FOR']))
    //to check ip is pass from proxy
    {
        $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

11. 强制性文件下载

为用户提供强制性的文件下载功能。

/********************
*@file - path to file
*/
function force_download($file)
{
    if ((isset($file))&&(file_exists($file))) {
       header("Content-length: ".filesize($file));
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="' . $file . '"');
       readfile("$file");
    } else {
       echo "No file selected";
    }
}

12. 创建标签云

function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 )
{
    $minimumCount = min( array_values( $data ) );
    $maximumCount = max( array_values( $data ) );
    $spread       = $maximumCount - $minimumCount;
    $cloudHTML    = '';
    $cloudTags    = array(); 
    $spread == 0 && $spread = 1; 
    foreach( $data as $tag => $count )
    {
        $size = $minFontSize + ( $count - $minimumCount )
            * ( $maxFontSize - $minFontSize ) / $spread;
        $cloudTags[] = '<a style="font-size: ' . floor( $size ) . 'px'
        . '" href="#" title="\'' . $tag  .
        '\' returned a count of ' . $count . '">'
        . htmlspecialchars( stripslashes( $tag ) ) . '</a>';
    } 
    return join( "\n", $cloudTags ) . "\n";
}
/**************************
****   Sample usage    ***/
$arr = Array('Actionscript' => 35, 'Adobe' => 22, 'Array' => 44, 'Background' => 43,
    'Blur' => 18, 'Canvas' => 33, 'Class' => 15, 'Color Palette' => 11, 'Crop' => 42,
    'Delimiter' => 13, 'Depth' => 34, 'Design' => 8, 'Encode' => 12, 'Encryption' => 30,
    'Extract' => 28, 'Filters' => 42);
echo getCloud($arr, 12, 36);

13. 寻找两个字符串的相似性

PHP 提供了一个极少使用的 similar_text 函数,但此函数非常有用,用于比较两个字符串并返回相似程度的百分比。

similar_text($string1, $string2, $percent);
//$percent will have the percentage of similarity

14. 在应用程序中使用 Gravatar 通用头像

随着 WordPress 越来越普及,Gravatar 也随之流行。由于 Gravatar 提供了易于使用的 API,将其纳入应用程序也变得十分方便。

/******************
*@email - Email address to show gravatar for
*@size - size of gravatar
*@default - URL of default gravatar to use
*@rating - rating of Gravatar(G, PG, R, X)
*/
function show_gravatar($email, $size, $default, $rating)
{
    echo '<img src="http://www.gravatar.com/avatar.php?gravatar_id='.md5($email).
        '&default='.$default.'&size='.$size.'&rating='.$rating.'" width="'.$size.'px"
        height="'.$size.'px" />';
}

(责任编辑:IT教学网)

更多