admin';?>

首页 / php code

php代码集锦

By admin  •  2020-01-30 12:08:42  •  208次点击
永久外链: https://i.otherhill.com/static/339179c8431611eab405809b202ce664.html

explode() function splits a string into an array using a separator.
explode(separator, $string, $limit)

//Turn array into a delimited string using
//the implode function
$data = implode(" - ", $dataToLog);

https://www.sunnyos.com/article-show-4.html
<?php
/*
*获取文章第一章图片
* $text string 查找图片的文章
* return string 图片路径
*/
function getpic($text){
    preg_match_all("#<img.*\>#isU",$text,$ereg);//先通过正则匹配出来所有的 
    $img=$ereg[0][0];//这里返回匹配到的数组
    preg_match_all ("#src=('|\")(.*)('|\")#isU", $img, $img1); //使用正则获取所有图片地址
    $img_path =$img1[2][0];//获取第一张图片路径
    return $img_path; 
}


php将image转换为base64编码:
<?php
$file = "example.jpg";
$type = getimagesize( $file ); //取得图片的大小,类型等
$file_content = base64_encode( file_get_contents( $file ) );
switch ( $type[2] ) { //判读图片类型
    case 1:
        $img_type = "gif";
        break;
    case 2:
        $img_type = "jpg";
        break;
    case 3:
        $img_type = "png";
        break;
}
$img = 'data:image/' . $img_type . ';base64,' . $file_content; //合成图片的base64编码

echo '<img src="' . $img . '" >';



empty() 与 isset() 的比较


empty()isset()(bool)
$var = ""TRUETRUEFALSE
$var = nullTRUEFALSEFALSE
$var = FALSETRUETRUEFALSE
var $varTRUEFALSEFALSE
$var = array()TRUETRUEFALSE
$var = 0TRUETRUEFALSE
$var = "0"TRUETRUEFALSE

可见 empty() 是比 isset() 对变量更严格的检查。

    isset():检测变量是否设置
    unset():释放给定的变量

UUID
for Linux:
file_get_contents('/proc/sys/kernel/random/uuid');
or
uniqid();

https://www.php.net/manual/en/function.com-create-guid.php
function GUID ($trim = true)
{
    // Windows
    if (function_exists('com_create_guid') === true) {
        if ($trim === true)
            return trim(com_create_guid(), '{}');
        else
            return com_create_guid();
    }

    // OSX/Linux
    if (function_exists('openssl_random_pseudo_bytes') === true) {
        $data = openssl_random_pseudo_bytes(16);
        $data[6] = chr(ord($data[6]) & 0x0f | 0x40);    // set version to 0100
        $data[8] = chr(ord($data[8]) & 0x3f | 0x80);    // set bits 6-7 to 10
        return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
    }

    // Fallback (PHP 4.2+)
    mt_srand((double)microtime() * 10000);
    $charid = strtolower(md5(uniqid(rand(), true)));
    $hyphen = chr(45);                  // "-"
    $lbrace = $trim ? "" : chr(123);    // "{"
    $rbrace = $trim ? "" : chr(125);    // "}"
    $guidv4 = $lbrace.
        substr($charid,  0,  8).$hyphen.
        substr($charid,  8,  4).$hyphen.
        substr($charid, 12,  4).$hyphen.
        substr($charid, 16,  4).$hyphen.
        substr($charid, 20, 12).
        $rbrace;
    return $guidv4;
}
echo GUID();

kindeditor/php/JSON.php
function alert($msg) {
	header('Content-type: text/html; charset=UTF-8');
	$json = new Services_JSON();
	echo $json->encode(array('error' => 1, 'message' => $msg));
	exit;
}

redirect
echo "<meta http-equiv='refresh' content='0;url=".site_url('topic/show/'.$topic_id)."'>";

0 回复 | 直到2022-10-01 01:08添加回复

回复

最近更新

私信给我
生成图片 生成二维码 生成密码
清空