记录一些php函数的用法

fopen和fgets配合使用按行读取

读取一个aaa.txt文件,每次按行读取,读一行删除一行

$nickNamePath = '/Users/ailuoy/Desktop/nickname.txt';
$nickNames = fopen($nickNamePath, 'r');
$nickName = fgets($nickNames);
ob_start();
fpassthru($nickNames);
fclose($nickNames);
file_put_contents($nickNamePath, ob_get_clean());

读取一个文件夹下的所有文件(不支持递归读取)

这个并不支持递归读取

$photoPath = '/Users/ailuoy/Desktop/photo';
// 设置过滤
$filter = array('.', '..','.DS_Store');
foreach (array_diff(scandir($photoPath), $filter) as $item) {
    $list[] = $photoPath.'/'.$item;
}

上传zip文件并解压

安装zip和zlib扩展

每次给我包我需要做的事情

  • 解压zip包
  • 上传到服务器
  • 修改数据库解压zip包的地址,以提供下载
/** 获得zip资源 */
$requestZip = request()->file('zip');
/** 读取保存目录和数据库存储目录 */
$dbPath = config('settings.upload_path.game_resource.dbPath').$game->game_id.'/';
$savePath = config('settings.upload_path.game_resource.savePath').$game->game_id.'/';
if(!is_dir($savePath)) mkdir($savePath,0775,true);

/** 获取文件名 */
$zipName = $requestZip->getClientOriginalName();
/** 获取后缀 */
$suffix = $requestZip->getClientOriginalExtension();

/** 实例化ZipArchive类,用于解压缩文件 */
$zipArchive = new ZipArchive();
/** Laravel方法保存文件 */
$requestZip->move($savePath,$zipName);
/** ZipArchive类打开文件 */
$zipArchive->open($savePath.$zipName);
/** ZipArchive解压文件 */
$zipArchive->extractTo($savePath);

$cdnDomain = config('settings.cdn_domain');
$tag = basename($zipName,'.'.$suffix);
$code_url = $cdnDomain.$dbPath.$tag.'/game_code_'.$tag.'.'.$suffix;
$update_url = $cdnDomain.$dbPath.$tag;
$gameStatus = $game->update([
    'update_url'=>$update_url,
    'code_url'=>$code_url
]);

压缩gif图

这里我们用Imagick扩展

yum安装Imagick扩展

yum search imagick
php71w-pecl-imagick.x86_64 : Provides a wrapper to the ImageMagick library
php72w-pecl-imagick.x86_64 : Provides a wrapper to the ImageMagick library
yum install php71w-pecl-imagick -y
systemctl restart php-fpm
➜  ~ php -m | grep imagick
imagick

压缩gif

一定要保证有写入权限

$image = new Imagick(public_path('uploads/gif_ttttt.gif'));
$height = $image->getImageHeight();
$width = $image->getImageWidth();
$image = $image->coalesceImages();
foreach ($image as $frame) {
    $frame->thumbnailImage($width/2,$height/2 ,true);
}
$image = $image->optimizeImageLayers();
$image->writeImages(public_path('uploads/new.gif'), true);

获取 url 链接的正则匹配

获取到的数据

需要将 url 链接提取出来

"""
#EXTM3U\n
#EXT-X-VERSION:3\n
#EXT-X-PLAYLIST-TYPE:VOD\n
#EXT-X-TARGETDURATION:10\n
#EXT-X-MEDIA-SEQUENCE:0\n
#EXTINF:1.985272,\n
https://cf-hls-media.sndcdn.com/media/0/31762/KBalstCILZZi.128.mp3?Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiKjovL2NmLWhscy1tZWRpYS5zbmRjZG4uY29tL21lZGlhLyovKi9LQmFsc3RDSUxaWmkuMTI4Lm1wMyIsIkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTUyNDczNTY1Mn19fV19&Signature=TwD6~6LwZVj6AJ4f8Xmvcd0RMWr6OnTBdHP54rZ83v3L-P90vQwZzfVtkAa~x8j5Q~Tw68mX4F5zRFz9MFPDMlwCx-ARYBaOB9C9R0Fy40bJhfiruxOeq~uX3xgnW9VmnpfRnayb8kvoG40twFkjGq89WkEjrB135XhMaMzd1i9cXPx5hbZ-PMNwMyHKrlHp8zA3E02Ufy2Jm~ZVCobN17ROggAEJhwJ7UHSUdD0Yjoa65X663uyo9dythiuYFN3gz9~Mki0fp3AcxWPLBWvnkEdxTZMcOxauFkCbvODpz4Lj5w5KeJARDtjWXKsKmMmw23d2ybel2YpVgI1btTzLA__&Key-Pair-Id=APKAJAGZ7VMH2PFPW6UQ\n
#EXTINF:2.977908,\n
https://cf-hls-media.sndcdn.com/media/31763/79410/KBalstCILZZi.128.mp3?Policy=eyJTdGF0ZW1lbnQiOlt7IlJlc291cmNlIjoiKjovL2NmLWhscy1tZWRpYS5zbmRjZG4uY29tL21lZGlhLyovKi9LQmFsc3RDSUxaWmkuMTI4Lm1wMyIsIkNvbmRpdGlvbiI6eyJEYXRlTGVzc1RoYW4iOnsiQVdTOkVwb2NoVGltZSI6MTUyNDczNTY1Mn19fV19&Signature=TwD6~6LwZVj6AJ4f8Xmvcd0RMWr6OnTBdHP54rZ83v3L-P90vQwZzfVtkAa~x8j5Q~Tw68mX4F5zRFz9MFPDMlwCx-ARYBaOB9C9R0Fy40bJhfiruxOeq~uX3xgnW9VmnpfRnayb8kvoG40twFkjGq89WkEjrB135XhMaMzd1i9cXPx5hbZ-PMNwMyHKrlHp8zA3E02Ufy2Jm~ZVCobN17ROggAEJhwJ7UHSUdD0Yjoa65X663uyo9dythiuYFN3gz9~Mki0fp3AcxWPLBWvnkEdxTZMcOxauFkCbvODpz4Lj5w5KeJARDtjWXKsKmMmw23d2ybel2YpVgI1btTzLA__&Key-Pair-Id=APKAJAGZ7VMH2PFPW6UQ\n
#EXTINF:4.989302,\n
.......
#EXT-X-ENDLIST

正则匹配方法

public function matchLinks ($document)
{

    preg_match_all('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', $document, $links);
    $match = [];
    while (list($key, $val) = each($links[2])) {
        if (!empty($val)) $match['link'][] = $val;
    }

    while (list($key, $val) = each($links[3])) {
        if (!empty($val)) $match['link'][] = $val;
    }

    while (list($key, $val) = each($links[4])) {
        if (!empty($val)) $match['content'][] = $val;
    }

    while (list($key, $val) = each($links[0])) {
        if (!empty($val)) $match['all'][] = $val;
    }

    return $match;

}

最终匹配结果

array:3 [▼
  "link" => array:25 [▶]
  "content" => array:25 [▶]
  "all" => array:25 [▶]
]
  • link 域名
  • content 不含域名的连接
  • all 完整url

php curl 分离header和body信息

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);    //表示需要response header
curl_setopt($ch, CURLOPT_NOBODY, FALSE); //表示需要response body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);

$result = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
    if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
        $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $header = substr($result, 0, $headerSize);
        $body = substr($result, $headerSize);
    }
}
return [$header,$body];

php正则过滤所有空格

preg_replace('# #','',$str)