# 第五课 字符串
> 字符长度
```
<pre class="calibre14">```
<span class="token1">strlen</span><span class="token2">(</span>$str<span class="token2">)</span> 中文utf8中<span class="token3">3</span>个长度 其他字符<span class="token3">1</span>个<span class="token2">,</span>gb2312中中文<span class="token3">2</span>个
<span class="token1">mb_strlen</span><span class="token2">(</span>$str1<span class="token2">,</span><span class="token4">'utf-8'</span><span class="token2">)</span><span class="token2">;</span>
```
```
> 截取,字符串截取,用于英文,长度每个汉字utf8占3字符
```
<pre class="calibre14">```
<span class="token1">substr</span><span class="token2">(</span>$str1<span class="token2">,</span><span class="token3">0</span><span class="token2">,</span><span class="token3">6</span><span class="token2">)</span><span class="token2">;</span> 开始位置<span class="token3">0</span><span class="token2">,</span>长度
<span class="token1">mb_substr</span><span class="token2">(</span><span class="token4">'曾经年少爱追梦一心只想往前飞'</span><span class="token2">,</span> <span class="token3">0</span><span class="token2">,</span> <span class="token3">7</span><span class="token2">,</span><span class="token4">'utf-8'</span><span class="token2">)</span><span class="token2">;</span>
```
```
> 查找替换,查找c替换成qqq
```
<pre class="calibre14">```
<span class="token1">str_replace</span><span class="token2">(</span><span class="token4">'c'</span><span class="token2">,</span><span class="token4">'qqq'</span><span class="token2">,</span><span class="token4">'abcdefg'</span><span class="token2">)</span><span class="token2">;</span>
```
```
> header
```
<pre class="calibre14">```
<span class="token1">header</span><span class="token2">(</span><span class="token4">"Content-Type:text/html;charset=UTF-8"</span><span class="token2">)</span><span class="token2">;</span>
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Content-type: text/css'</span><span class="token2">)</span><span class="token2">;</span> css
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Content-type: text/javascript'</span><span class="token2">)</span><span class="token2">;</span> js
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Content-type: image/jpeg'</span><span class="token2">)</span><span class="token2">;</span> jpg
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Content-type: application/json'</span><span class="token2">)</span><span class="token2">;</span> json
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Content-type: text/plain'</span><span class="token2">)</span><span class="token2">;</span> txt
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Content-type: text/xml'</span><span class="token2">)</span><span class="token2">;</span> xml
<span class="token1">header</span><span class="token2">(</span><span class="token4">'HTTP/1.1 200 OK'</span><span class="token2">)</span><span class="token2">;</span> <span class="token3">200</span> 正常ok
<span class="token1">header</span><span class="token2">(</span><span class="token4">"HTTP/1.1 404 Not Found"</span><span class="token2">)</span><span class="token2">;</span>未找到
<span class="token1">header</span><span class="token2">(</span><span class="token4">"http/1.1 403 Forbidden"</span><span class="token2">)</span><span class="token2">;</span>无权限
<span class="token1">header</span><span class="token2">(</span><span class="token4">"http/1.1 301 Moved Permanently"</span><span class="token2">)</span><span class="token2">;</span>重定向
<span class="token1">header</span><span class="token2">(</span><span class="token4">'HTTP/1.1 304 Not Modified'</span><span class="token2">)</span><span class="token2">;</span>没改变
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Location: http://www.example.org/'</span><span class="token2">)</span><span class="token2">;</span>
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Refresh: 10; url=http://www.example.org/'</span><span class="token2">)</span><span class="token2">;</span>
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Content-Disposition: attachment; filename="downloaded.pdf"'</span><span class="token2">)</span><span class="token2">;</span>
跨域
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Access-Control-Allow-Origin: *'</span><span class="token2">)</span><span class="token2">;</span>
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Access-Control-Allow-Headers:content-type'</span><span class="token2">)</span><span class="token2">;</span>
<span class="token1">header</span><span class="token2">(</span><span class="token4">'Content-Type:application/json; charset=utf-8'</span><span class="token2">)</span><span class="token2">;</span>
```
```
> serialize 和unserialize 序列化和反序列化
> json\_encode($arr)和json\_decode($json,TRUE)true是返回数组 默认对象
json\_encode($arr,JSON\_UNESCAPED\_UNICODE ) 编码后还是中文
> urlencode 和urldecode url编码和解码
```
<pre class="calibre14">```
<span class="token1">mb_convert_encoding</span><span class="token2">(</span><span class="token4">'李太白'</span><span class="token2">,</span><span class="token4">"utf-8"</span><span class="token2">,</span><span class="token4">'auto'</span><span class="token2">)</span><span class="token2">;</span>自动转换成uft8
<span class="token1">iconv</span><span class="token2">(</span><span class="token4">'utf-8'</span><span class="token2">,</span><span class="token4">'gb2312//IGNORE'</span><span class="token2">,</span><span class="token4">'礼拜是谁'</span><span class="token2">)</span><span class="token2">;</span>转换成gb2312
```
```
> strtolower转换小写 strtoupper转换成大写
```
<pre class="calibre14">```
<span class="token1">strtolower</span><span class="token2">(</span><span class="token4">'wEws2'</span><span class="token2">)</span><span class="token2">;</span>
```
```
> strip\_tags($rs0); 去除标签
> error\_reporting(0);禁止显示错误
> error\_reporting(E\_ALL)显示错误
```
<pre class="calibre14">```
<span class="token1">pathinfo</span><span class="token2">(</span>__FILE__<span class="token2">)</span>
Array
<span class="token2">(</span>
<span class="token2">[</span>dirname<span class="token2">]</span> <span class="token">=</span><span class="token">></span> C<span class="token2">:</span>\www
<span class="token2">[</span>basename<span class="token2">]</span> <span class="token">=</span><span class="token">></span> Codelobster1<span class="token2">.</span>php
<span class="token2">[</span>extension<span class="token2">]</span> <span class="token">=</span><span class="token">></span> php
<span class="token2">[</span>filename<span class="token2">]</span> <span class="token">=</span><span class="token">></span> Codelobster1
<span class="token2">)</span>
```
```
添加和去除转义符
```
<pre class="calibre14">```
<span class="token1">addslashes</span><span class="token2">(</span><span class="token4">"wo \' \"ni是"</span><span class="token2">)</span><span class="token2">;</span> 入库添加反斜杠
<span class="token1">stripslashes</span><span class="token2">(</span><span class="token4">"wo \' \"ni是"</span><span class="token2">)</span><span class="token2">;</span>
```
```
> 转换实体和还原
```
<pre class="calibre14">```
<span class="token1">htmlspecialchars_decode</span><span class="token2">(</span>$str<span class="token2">)</span>
<span class="token1">htmlspecialchars</span><span class="token2">(</span><span class="token4">"你是"</span><span class="token2">,</span>ENT_NOQUOTES<span class="token2">,</span><span class="token4">'UTF-8'</span><span class="token2">)</span><span class="token2">;</span>
```
```
> 检测扩展是否存在,curl,gd,openssl
```
<pre class="calibre14">```
<span class="token1">extension_loaded</span><span class="token2">(</span><span class="token4">'memcache'</span><span class="token2">)</span><span class="token2">;</span>
<span class="token1">function_exists</span><span class="token2">(</span><span class="token4">'curl_init'</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//检测函数是否存在</span>
<span class="token1">class_exists</span><span class="token2">(</span><span class="token4">'MyClass'</span><span class="token2">)</span><span class="token2">)</span> 检测类是否存在
<span class="token1">get_extension_funcs</span><span class="token2">(</span><span class="token4">"xml"</span><span class="token2">)</span><span class="token2">;</span> 显示所有方法
<span class="token1">get_loaded_extensions</span><span class="token2">(</span><span class="token2">)</span> 显示所有扩展
<span class="token1">get_defined_constants</span><span class="token2">(</span><span class="token3">true</span><span class="token2">)</span><span class="token2">[</span><span class="token4">'user'</span><span class="token2">]</span>一已定义常量
$arr <span class="token">=</span> <span class="token1">get_defined_functions</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span>$arr<span class="token2">[</span><span class="token4">'user'</span><span class="token2">]</span> 已定义函数
```
```
> 如果自动添加转义符没有打开则使用以下
```
<pre class="calibre14">```
<span class="token5">if</span><span class="token2">(</span><span class="token">!</span><span class="token1">get_magic_quotes_gpc</span><span class="token2">(</span><span class="token2">)</span><span class="token2">)</span><span class="token2">{</span>
<span class="token1">addslashes</span><span class="token2">(</span>$var<span class="token2">)</span>
<span class="token2">}</span>
```
```
> 解析url
```
<pre class="calibre14">```
<span class="token1">parse_url</span><span class="token2">(</span><span class="token4">"http://www.baidu.com/4/1.php?qq=1&d=1#123"</span><span class="token2">)</span><span class="token2">;</span>
Array
<span class="token2">(</span>
<span class="token2">[</span>scheme<span class="token2">]</span> <span class="token">=</span><span class="token">></span> http
<span class="token2">[</span>host<span class="token2">]</span> <span class="token">=</span><span class="token">></span> www<span class="token2">.</span>baidu<span class="token2">.</span>com
<span class="token2">[</span>path<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token">/</span><span class="token3">4</span><span class="token">/</span><span class="token3">1.</span>php
<span class="token2">[</span>query<span class="token2">]</span> <span class="token">=</span><span class="token">></span> qq<span class="token">=</span><span class="token3">1</span><span class="token">&</span>d<span class="token">=</span><span class="token3">1</span>
<span class="token2">[</span>fragment<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">123</span>
<span class="token2">)</span>
```
```
> 解析字符串
```
<pre class="calibre14">```
<span class="token1">parse_str</span><span class="token2">(</span><span class="token4">"id=23&name=John%20Adams"</span><span class="token2">,</span>$arr<span class="token2">)</span><span class="token2">;</span>输出$arr
Array
<span class="token2">(</span>
<span class="token2">[</span>id<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">23</span>
<span class="token2">[</span>name<span class="token2">]</span> <span class="token">=</span><span class="token">></span> John Adams
<span class="token2">)</span>
```
```
变量数组互换
```
<pre class="calibre14">```
$arr <span class="token">=</span> <span class="token1">array</span><span class="token2">(</span><span class="token4">'qq'</span><span class="token">=</span><span class="token">></span><span class="token3">1</span><span class="token2">,</span><span class="token4">'ss'</span><span class="token">=</span><span class="token">></span><span class="token3">5</span><span class="token2">)</span><span class="token2">;</span>
<span class="token1">extract</span><span class="token2">(</span>$arr<span class="token2">)</span><span class="token2">;</span><span class="token6">//转换成变量</span>
$a<span class="token">=</span><span class="token3">1</span><span class="token2">;</span>$b<span class="token">=</span><span class="token3">2</span><span class="token2">;</span>
$arr <span class="token">=</span> <span class="token1">compact</span><span class="token2">(</span><span class="token4">'a'</span><span class="token2">,</span><span class="token4">'b'</span><span class="token2">)</span><span class="token2">;</span>
```
```
> 获取输入,post可用
```
<pre class="calibre14">```
$str<span class="token">=</span><span class="token1">file_get_contents</span><span class="token2">(</span><span class="token4">'php://input'</span><span class="token2">)</span><span class="token2">;</span> 等同$_POST<span class="token2">,</span>用于
```
```
> $\_SERVER
```
<pre class="calibre14">```
OS系统
<span class="token2">[</span>HTTP_HOST<span class="token2">]</span> <span class="token">=</span><span class="token">></span> localhost 域名
<span class="token2">[</span>HTTP_USER_AGENT<span class="token2">]</span> <span class="token">=</span><span class="token">></span> Mozilla<span class="token">/</span><span class="token3">5.0</span> <span class="token2">(</span>Windows NT <span class="token3">6.2</span><span class="token2">;</span> WOW64<span class="token2">;</span> Trident<span class="token">/</span><span class="token3">7.0</span><span class="token2">;</span> rv<span class="token2">:</span><span class="token3">11.0</span><span class="token2">)</span> like Gecko
<span class="token2">[</span>SERVER_SOFTWARE<span class="token2">]</span> <span class="token">=</span><span class="token">></span> nginx<span class="token">/</span><span class="token3">1.9</span><span class="token3">.9</span>
<span class="token2">[</span>REQUEST_METHOD<span class="token2">]</span> <span class="token">=</span><span class="token">></span> GET
<span class="token2">[</span>SCRIPT_FILENAME<span class="token2">]</span> <span class="token">=</span><span class="token">></span> C<span class="token2">:</span><span class="token">/</span>www<span class="token">/</span>Codelobster1<span class="token2">.</span>php
<span class="token2">[</span>SERVER_ADDR<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">127.0</span><span class="token3">.0</span><span class="token3">.1</span>
<span class="token2">[</span>REMOTE_ADDR<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token3">127.0</span><span class="token3">.0</span><span class="token3">.1</span>
<span class="token2">[</span>REQUEST_URI<span class="token2">]</span> <span class="token">=</span><span class="token">></span> <span class="token">/</span>Codelobster1<span class="token2">.</span>php<span class="token">?</span>qq<span class="token">=</span><span class="token3">1</span>
```
```
> 生成id
```
<pre class="calibre14">```
<span class="token1">uniqid</span><span class="token2">(</span><span class="token2">)</span> 默认<span class="token3">13</span>
<span class="token1">uniqid</span><span class="token2">(</span><span class="token4">'y_'</span><span class="token2">,</span><span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> <span class="token3">25</span> 没有 y_则是<span class="token3">23</span>
```
```
> 字符串反序
> `strrev("abc")`
去除html标签
`strip_tags($str)`
- 简介
- 第一章 数据库
- Mysql/mariadb
- 函数
- 基础
- 增删改索引
- 标准查询
- 高级查询
- TIDB集群mysql解决方案
- Redis
- 语言基础
- 5种数据类型
- 其他类型
- Sqlite
- 语言基础
- 常用查询
- 第二章 PHP
- 语言基础
- 第一课 流程控制和运算
- 第二课 数组
- 第三课 日期时间
- 第四课 常用函数
- 第五课 字符串
- 第六课 文件操作
- 第七课 面向对象
- 第八课 正则表达式
- 第九课 图片处理生成
- 第十课 curl/memche
- 第十一课 mysql和pdo
- 第十三课 cookie和session
- 第十四课 xml操作
- 第十五课 php5.3+新特性
- 第十六课 php7+
- 第十七课 密码安全
- 废弃函数
- php命令行
- redis应用
- 算法
- 排序算法
- 基础算法
- 无限级分类
- 自定义函数Fn
- 查找算法
- 自定义函数数据函数fn
- laravel
- 路由
- 常用语句
- 数据库
- dingo/api
- Yii2
- 控制器
- 常用类
- 数据库
- redis
- thinkphp6
- TP6文档
- TP6插件
- dedecms
- 织梦标签大全
- 数据库操作
- 内置函数和定义函数
- 织梦核心改动
- 织梦插件/底层标签开发
- PHP相关工具
- composer
- php开发环境phpenv
- Phpstorm使用
- windows编译php扩展
- PHP开源库
- 开源项目管理禅道
- sns_auth
- php-casbin权限控制
- php-jwt
- 微信SDKeasywechat
- querylist采集库
- workerman
- Box/Spout处理excel和csv
- dll扩展
- redis/memche/xdebug
- redis
- Lua
- php_xlswriter
- event
- swoole
- 常用代码库
- 微擎框架
- 第一课全局变量
- 第二课常用函数
- 第三课自定义微擎独有函数
- 第四课数据库操作
- 第五课微信端回复
- 第六课微擎高级操作
- 第八课global函数列表
- mainfest.xml详解
- js方法
- 人人商城
- 第一课model解读
- 第二课常用语句解读
- 第三课常用js解读
- 第四课附录常见问题
- 第五课附录处理报表|支付
- 常用JSON状态码
- 第三章 JavaScript
- js基础
- 浏览器对象
- 语言基础
- html5接口
- ES6新语法
- vue
- 基础语法
- 京东vueUI组件
- uniapp
- 组件开发规范
- nodejs
- 基础知识
- 安装node
- nvm不同版本node切换
- js常用标准库
- zepto/jquery
- weui
- js图标库
- elementUI
- validator表单验证
- layer弹出层
- requirejs
- wow动画
- 动画animate
- swiper4
- 百度编辑器
- flyio/axios/qs
- jquery.form
- bootstrap3
- clipboard复制
- slideout侧滑
- imagehover.css图片悬停动画
- webpack打包
- Bulma UI框架
- store 客户端存储
- lottie动画创建库
- sweetalert
- js自定义函数
- 常见JSSDK
- 微信公众号JSSDK
- 腾讯地图jssdk
- 微信小程序
- 第四章 编程语言
- markdown语言
- Dart语言
- Dart语言基础
- Flutter框架
- Lua语言
- 字符串,数组,表
- 自定义方法
- go语言
- 第1.1语言基本语法
- 第1.2流程控制
- 第1.3函数
- 第1.4结构体
- 第1.5接口
- 第1.6包
- go语言框架Gin
- CSS3语言
- CSS与CSS3
- 选择符
- 属性
- css3
- loading动画
- HTML5语言
- less
- sass
- C#
- 基础知识
- 函数
- 第五章 开发工具
- git
- nginx/apache服务器
- Linux常用操作
- crontab定时任务
- 注册表与cmd
- 阿里云ECS
- frp穿透和ssl续期
- 宝塔安装
- 树莓派
- 浏览器模拟
- 火狐/chrome常用插件
- WSL安装使用
- mac brew和终端命令
- win10相关