小墨の博客

梦想需要付诸行动,否则只能是梦

PHP开发常用代码

PHP开发常用代码

php压缩CSS代码

将以下代码放置于 style.css.php 文件中,不要忘记包含你需要的 css 文件:

<?php
header('Content-type: text/css');
ob_start("compress");
function compress($buffer) {
  /* remove comments */
  $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
  /* remove tabs, spaces, newlines, etc. */
  $buffer = str_replace(array("
", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
  return $buffer;
}
 
/* your css files */
include('master.css');
include('typography.css');
include('grid.css');
include('print.css');
include('handheld.css');
 
ob_end_flush();

然后在 HTML 页面中在引入样式的地方引入该php文件:

<link rel="stylesheet" type="text/css" media="screen" href="style.css.php"/>

php获取带http或https的域名

$url = (isHTTPS() ? 'https://' : 'http://') . $_SERVER['HTTP_HOST']; //获取域名
//判断是否是HTTPS
function isHTTPS()
{
    if (defined('HTTPS') && HTTPS) return true;
    if (!isset($_SERVER)) return FALSE;
    if (!isset($_SERVER['HTTPS'])) return FALSE;
    if ($_SERVER['HTTPS'] === 1) {  //Apache
        return TRUE;
    } elseif ($_SERVER['HTTPS'] === 'on') { //IIS
        return TRUE;
    } elseif ($_SERVER['SERVER_PORT'] == 443) { //其他
        return TRUE;
    }
    return FALSE;
}

使用PHP对文件进行base64编码

base64EncodeImage

<?php
// 使用PHP对文件进行base64编码
$img = "./img.png";
$base64_img = base64EncodeImage($img);
  
function base64EncodeImage ($image_file) {
  $base64_image = '';
  $image_info = getimagesize($image_file);
  $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
  $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
  return $base64_image;
}
?>

php 获取各级域名

<?php 
// 获取各级域名

// 如果php文件所在地址为:【https://temp.only4.work/test.php】

echo $_SERVER["HTTP_HOST"]; // 【temp.only4.work】

echo '<br>';

$url=$_SERVER['HTTP_HOST'];
preg_match("#\.(.*)#i",$url,$match);//获取根域名
$domin = $match[1];
echo $domin; // 【only4.work】

echo '<br>';

preg_match("#(.*?)\.#i",$url,$match);//获取二级域名
$domin = $match[1];
echo $domin; // 【temp】

echo '<br>';

// 获取域名后可根据业务需求对域名进行重定向
switch ($_SERVER["HTTP_HOST"]) 
{
case "要判断的域名":
header("location:跳转路径");
break;
case "要判断的域名":
header("location:跳转路径"); 
break; 
}
?>

php 301 302 重定向

301:永久移动,302:临时移动

把Location后面的url替换成你想跳转的url即可

<?php
    // 301:永久移动
    header('HTTP/1.1 301 Moved Permanently');
    header('Location:https://www.only4.work/');
    exit();
?>

<?php
    // 302:临时移动
    header('Location:https://www.only4.work/');
    exit();
?>

PHP禁止本文件被单独访问(可以include、require)

一般做程序,我们为了让代码简洁或各种原因会把需要的代码单独保存为一个文件在include或require,所以我们就要避免这些东西被单独访问。将这段代码放到你的程序中,就可以不被访问但可以include和require了。

<?php
if(basename($_SERVER['PHP_SELF']) == basename(__FILE__)) return;
?>

php 获取路径的目录结构并显示

<?php
// $ignoreDir = ['.git', '.idea', 'vendor', '.vscode', 'logs', 'uploads', 'static', 'public'];
$ignoreDir = ['.git', '.idea', '.vscode'];
$OutPutString = "";
function loopDir($dir)
{
    global $ignoreDir;
    global $OutPutString;
    static $level = 0;
    $handle       = opendir($dir);
    while (false !== ($file = readdir($handle))) {
        if ($file != '.' && $file != '..') {
            $str = '';
            for ($i = 0; $i < $level; $i++) {
                $str .= '  ';
            }
            if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) {

                if (!in_array($file, $ignoreDir)) {

                    $OutPutString .= $str . ($level > 0 ? '  - ' : '- ') . $file . PHP_EOL;
                    ++$level;
                    loopDir($dir . DIRECTORY_SEPARATOR . $file);
                }

            }
        }
    }
    $level--;
    closedir($handle);
}

loopDir(__DIR__);
echo '<pre>'.$OutPutString.'</pre>';

效果:

- License
    - css
    - js
- static
    - css
    - js
      - jquery


张小弟之家

本文链接:
文章标题:

本站文章除注明转载/出处外,均为原创,若要转载请务必注明出处。转载后请将转载链接通过邮件告知我站,谢谢合作。本站邮箱:admin@only4.work

尊重他人劳动成果,共创和谐网络环境。点击版权声明查看本站相关条款。

    发表评论:

    搜索
    本文二维码
    标签列表
    站点信息
    • 文章总数:518
    • 页面总数:20
    • 分类总数:92
    • 标签总数:209
    • 评论总数:63
    • 浏览总数:244305

    | | |
    | |  Z-Blog PHP