这个需求是源于使用LATEX排版中文时出现的。CJK宏包对中文字体的支持比较麻烦,已经不再推荐使用,但是,一些老项目还在用,不想动它。CJK自带的UTF-8编码字体gbsn和gkai只包含GB2312字符集,这就导致有些生僻字会出现无法显示的情况。对于一些检测报告,要是姓名有生僻字没有显示出来的话,这会导致很严重的后果。所以想要有个预警机制,判断姓名里面是否包含生僻字,也就是判断一个汉字是否在GB2312汉字编码字符集里面。iconv函数用于在不同字符编码之间进行转换,可将一个字符串从一种字符编码转换成另一种字符编码,比如将UTF-8编码的字符串转换成GBK编码的字符串。错误控制运算符@将屏蔽iconv函数可能产生的notice提示。实现代码<?php function isGB2312($char) { if (@iconv('UTF-8', 'GB2312', $char)) { echo $char . ":在GB2312里面\n"; } else { echo $char . ":不在GB231
基本使用默认情况下常量、类和函数名都放在全局空间下。命名空间通过namespace来声明,必须定义在文件头部,并在declare(strict_types=1) 语句下面。文件a.php<?php namespace A; function test() { echo 'test-A'; }文件b.php<?php namespace B; function test() { echo 'test-B'; }文件index.php<?php include 'a.php'; include 'b.php'; A\test(); B\test();引入方式非限定名称调用类时没有指定命名空间时,将使用当前命名空间。限定名称类似于文件系统中的相对路径。文件a.php<?php namespace A\B; class Test { public static function test() { echo __METHOD__; } }文件b.php<?php namespace A; include 'a.php'
$this self:: parent::$this是当前对象的引用,一般出现在方法里,用于获取类的成员属性,或执行类的成员方法。self::是对本类的引用,用于获取当前类的静态成员属性或静态成员方法。parent::是对父类的引用。用于调用父类的方法或属性。类常量使用const来定义类常量,使用self::来调用类常量。<?php class A { const YES = 1; public function aa() { return self::YES; } } echo (new A)->aa(); //1方法重写子类可以重写父类的方法,但使用final声明的方法,将禁止在子类中重写父类方法。抽象类具有抽象方法的类为抽象类,抽象方法即为没有内容的空方法,要求子类进行完善内容,抽象类不能实例化,只能继承。继承一个抽象类的时候,方法的可见性必须和抽象类中一样(或者更为宽松)。如果抽象类中某个抽象方法被声明为protected,那么子类中实现的方法就应该声明为protected或者public,而不能定义private。<?
microtime返回当前时间戳以及微秒数。<?php function runtime($start = null, $end = null) { static $cache = []; if (is_null($start)) { return $cache; } elseif (is_null($end)) { return $cache[$start] = microtime(true); } else { $end = $cache[$end] ?? microtime(true); return round($end - $cache[$start], 3); } } runtime('for'); for ($i = 0; $i < 10000000; $i++) { $i++; } runtime('forEnd'); echo 'for循环用的时间:' . runtime('for', 'forEnd');strtotime将任何英文文本日期时
面向过程<?php $servername = 'localhost'; $username = 'root'; $password = 'root'; try { $conn = mysqli_connect($servername, $username, $password); echo '连接成功'; } catch (Exception $e) { die('连接失败:' . $e->getMessage()); } mysqli_close($conn);面向对象<?php $servername = 'localhost'; $username = 'root'; $password = 'root'; try { $conn = new mysqli($servername, $username, $password); echo '连接成功'; } catch (Exception $e) { die('连接失败:' . $e->getMessage()); } $conn->clos
strlen()获取字符串长度。获取多字节字符串的长度使用mb_strlen()函数:<?php $s = 'abc'; $s2 = '张三'; echo strlen($s); //3 echo strlen($s2); //6 echo mb_strlen($s2, $encoding = 'utf8'); //2strpos()在字符串内查找一个字符或一段指定的文本,返回第一次出现的位置或false。stripos()同上,但不区分大小写strrpos()同上上,返回最后一次出现的位置或falsestrripos()同上,但不区分大小写explode()把字符串打散成数组,implode()把数组拼接成字符串。<?php $str = 'ab cd ef'; $str2 = explode(' ', $str); $str3 = implode('-', $str2); print_r($str2); print_r($str3);strtoupper()把字符串转换为大写,strtolower()把字符串转换为小写,ucfirst()将单词的首字母转换为大写,lcf
官网PHP官网:https://www.php.net/文档下载:https://www.php.net/distributions/manual/php_enhanced_zh.chmecho和printecho能够输出一个以上的字符串,逗号隔开print只能输出一个字符串,并始终返回1echo比print稍快,并且开销低CLIPHP CLI是PHP语言的命令行解释器。<?php echo '请输入内容:'; $input = fgets(STDIN); echo '输入内容是:' . $input;执行脚本:php test.php。交互式Shell$ php -a Interactive shell php > echo PHP_OS; WINNT php > echo PHP_VERSION; 7.3.4数据类型用var_dump($a);会返回变量的数据类型和值,一般开发调试时使用,只获取数据类型用echo gettype($a);。字符串<?php $s = 'abc'; echo '$s'; //$s echo "$s"; /
背景为什么抛弃WordPress?网站访问量极少,大部分是自用记录些笔记不想再买服务器了为什么选择Hugo?Hugo编译速度贼快有一款喜欢的Hugo主题从WordPress导出数据在 WordPress 仪表盘中,[工具] -> [导出] -> [下载导出的文件]。搭建临时Hexo博客安装node.jsnode.js官方下载地址:https://nodejs.org/en/设置npm淘宝镜像站:npm config set registry https://registry.npm.taobao.org。切回官方镜像用:npm config set registry http://www.npmjs.org。安装GitGit官方下载地址:https://git-scm.com/downloads安装Hexo运行下列命令安装Hexo:npm install -g hexo新建一个Hexo博客:hexo init <folder> cd <folder> npm install再安装 hexo-migrator-wordpress 插件:npm inst
简介PHPWord 是一个用纯 PHP 编写的库,它提供了一组用于写入和读取不同文档文件格式的类。项目地址:https://github.com/PHPOffice/PHPWord使用// 引入PHPWord类库 require '../vendor/autoload.php'; use \PhpOffice\PhpWord\PhpWord; // 创建Word文档 $phpWord = new PhpWord(); // 设置默认字体和字号 $phpWord->setDefaultFontName('宋体'); $phpWord->setDefaultFontSize(14); //四号字体 // 定义一级标题样式 $phpWord->addTitleStyle(1, ['size' => 16, 'bold' => true]); //三号字体 // 添加段落 $section = $phpWord->addSection(); $section->addText('添加文本', ['size' => 18, 'bold' =&g
当Fedora安装多个PHP版本的时候,默认的php命令会使用/usr/bin/php。cd /usr/bin/ ll php*如果不想改变命令名字,可以直接把别的版本的可执行文件覆盖/usr/bin/php这个文件,例如:mv php php.bak cp /opt/remi/php74/root/usr/bin/php php
目前Fedora 37软件中支持的PHP稳定版本为PHP 8.1版本。可以通过运行dnf命令来确认:dnf info php-fpm。PHP 8安装要从Fedora软件仓库安装默认的PHP版本,请使用下面的命令:sudo dnf -y install php-cli php-fpm php-gd php-curl php-mysqlnd php-zip php-xml php-mbstring php-bcmath安装完,查看PHP版本号:php -v。PHP 7.4安装由于官方Fedora软件仓库只提供PHP 8,所以必须添加另一个存储库才能安装旧版本的PHP。这个仓库被称为REMI仓库。将REMI仓库添加到Fedora 37系统:sudo dnf -y install https://rpms.remirepo.net/fedora/remi-release-37.rpmREMI仓库中有一些常见的依赖项,需要启用:sudo dnf config-manager --set-enabled remi sudo dnf module reset php开始安装PHP 7.4:sudo
因为Typecho的机制,页面、附件都要占cid。而且点击一次创建文章cid就会增加一次,即使你没有书写内容,那个cid也被认为已经使用了,所以下一次生成的cid就会跳过那个,造成不连续。虽然不影响什么,也无关紧要,但是对于有强迫症的人来说,真的是无法忍受,看到了非得给改一下不可。特别提醒:请在PHP 7以上版本的服务器上执行。本操作涉及数据库,请提前做好备份工作。文章cid将以下代码保存为php文件并上传至网站根目录,在浏览器地址栏访问一下即可。<?php /** * Typecho重新排列文章cid让他连续 */ // 设置数据库参数 $hostname_blog = "localhost"; $database_blog = "数据库名"; $username_blog = "数据库用户名"; $password_blog = "数据库密码"; // 连接到数据库 $blog = mysqli_connect($hostname_blog, $username_blog, $password
删除安装文件Typecho成功安装后,删除根目录下的install.php文件、install文件夹。修改后台地址将根目录下的admin文件夹改名为黑客猜不到的名字,例如google,防止黑客穷举密码。然后修改根目录下config.inc.php文件中后台路径配置:// admin directory (relative path) define('__TYPECHO_ADMIN_DIR__', '/google/');之后访问后台就用你的域名/google/,原来的你的域名/admin/则不能访问。修改文件权限屏蔽usr、var目录下php文件的访问可阻止黑客访问到他上传的php木马。这里利用Rewrite伪静态机制来做,以Apache服务器为例,同时屏蔽config.inc.php和.htaccess的访问。屏蔽原理是把要屏蔽的请求重定向到首页文件,首页文件会被当成文章名来解析,没有同名文章就会返回404未找到。所以就算黑客上传了木马也只会得到404未找到的响应。新建.htaccess文件添加以下内容,上传到typecho博客根目录。<IfModule mod_rewrite
从WordPress转到Typecho,首先要建立Typecho博客,建好后,首要的就是导入数据,这里选用Wordpress To Typecho插件直接将WordPress文章、评论、分类、标签等全部导入Typecho。数据迁移下载插件Wordpress To Typecho,并上传到 /usr/plugins 目录下。这个插件是Typecho官方2014年前发布的,插件版本为(v1.0.3 Beta),仅适用于WordPress 2.7,但实际上WordPress 6.3也能用。登录Typecho后台,进入菜单【控制台】→【插件】,将Wordpress to Typecho插件启用进入该插件的 设置 界面,填好WordPress所在数据库的地址、端口、用户名、密码、数据库名称、表前缀,并【保存设置】进入菜单【控制台】→【从Wordpress导入数据】,此时会看到导入提示界面,点击按钮【开始数据转换】即可当系统提示转换成功后,数据就全部迁移到了Typecho完成后,可以禁用或删除这个插件如果转换失败,系统会给出失败原因,很多情况是因为数据库设置错误导致的,只需重新设置 WordPre
本地开发环境下载并安装Local:https://localwp.com/打开 Local,点击「+ Create a new site],输入网站名称(例如:test),点击「Continue」->「Continue」,输入用户名、密码、邮箱,点击「Add Site」,稍等片刻,网站就安装完成了。点击「Open site」,即可打开网站。可能出现打不开网站的情况。如果之前安装过 VMware Workstation,端口 443 会存在冲突。打开「服务」应用,找到「VMware Workstation Server」,右键「属性],启动类型改为「手动」。创建新主题在网站上右键「Go to site folder」,打开网站文件路径,进到test/app/public/wp-content/themes,新建文件夹(例如:mytheme),进入 mytheme/,新建文件 index.php、style.css,所有主题都需要这两个文件。在文件style.css写入:/* Theme Name: My Theme Author: GAGA Version:
因服务器不在国内的缘故,网站没有很好的速度,所以苦苦钻研加速之道。Github创建Github仓库登录到Github之后点击右上角头像旁边的「+」,选择new repository创建新仓库在仓库创建页面 ,填写仓库名,其他默认就行通过Git同步到Github进入要托管的网站目录,创建.gitignore文件(https://www.toptal.com/developers/gitignore):cd 站点目录 touch .gitignore vi .gitignore把以下内容复制到.gitignore:## 排除文件目录 *.php *.html *.xml *.po *.mo *.json *.txt *.htaccess *.zip *.gz *.phtml *.html.gz *.bmp *.md !/wp-content/cache/ !/wp-content/upload/通过Git同步静态文件到Github:git init #初始化git git add -A wp-content wp-includes .gitignore # 添加要上传的文件 git com
三毛
头发渐少,仅剩三根