PHP中常用的十个字符串函数

  • explode
  • implode
  • trim
  • str_replace
  • strlen
  • strip_tags
  • addslashes
  • strrev
  • urlencode
  • strpos

explode

【ikˈsplōd】
使用字符串做为另一个字符串的分隔符,返回包含分割结果的数组。

<?php@see https://www.php.net/manual/zh/function.explode.php
// explode ( string $delimiter , string $string [, int $limit ] ) : arrayprint_r(explode(' ','a b c d'));Array
([0] => a[1] => b[2] => c[3] => d
)

implode

【imˈplōd】
将一个一维数组转为字符串。

<?php@see https://www.php.net/manual/zh/function.implode.php
// implode ( string $glue , array $pieces ) : string$array = ['a','b','c','d'];
print_r(implode('', $array));
// abcd

trim

【trɪm】
去除字符串首尾的某些字符。
该函数接收两个参数,第一个为要处理的字符串,第二个为要去除的首尾字符。
第二个参数默认为" \t\n\r\0\x0B",可自行指定。

<?php@see https://www.php.net/manual/zh/function.trim.php
// trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] ) : string$string = ' content';
echo $string;
echo trim($string);直接输出内容为" content"
使用trim函数后输出内容为"content"// 自定义去除字符
$string = '12content12';
echo trim($string, '12');
// content

str_replace

【str_riˈplās】

在一个字符串或数组中搜索并替换,返回替换后的字符串或数组。

<?php@see https://www.php.net/manual/zh/function.str-replace.php
// str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) : mixed$string = 'content';
echo str_replace('t', 'T', $string);
// conTenT// 数组替换
$array = ['content1','content2'];
print_r(str_replace('t', 'T', $array));Array
([0] => conTenT1[1] => conTenT2
)

strlen

获取字符串长度

<?php@see https://www.php.net/manual/zh/function.strlen.php
// strlen ( string $string ) : intecho strlen('content');
// 7

strip_tags

从字符串中去除 HTML 和 PHP 标记。
第一个参数为要处理的字符串,第二个可选参数为不去除的标记白名单。

<?php@see https://www.php.net/manual/zh/function.strip-tags.php
// strip_tags ( string $str [, string $allowable_tags ] ) : stringecho strip_tags('<span>content</div><script>', '<span>');
// <span>content

addslashes

使用反斜线转义单引号、双引号、反斜线与NULL。

<?php@see https://www.php.net/manual/zh/function.addslashes.php
// addslashes ( string $str ) : string$string = "content'";
echo addslashes($string);
// content\'

strrev

反转字符串

<?php@see https://www.php.net/manual/zh/function.strrev.php
// strrev ( string $string ) : string$string = "content";
echo strrev($string);
// tnetnoc

urlencode

URL需要遵守统一编码规范,服务端才能收到客户端想传递的正确数据。
除了大小写字母、数组和一些符号,其他例如空格、中文等等都会进行编码,会将ASCII码集之外的转换为%加上相应的16进制。

<?php@see https://www.php.net/manual/zh/function.urlencode.php
// urlencode ( string $str ) : string$string = "?wd=asdf 中文+-=";
echo urlencode($string);
// %3Fwd%3Dasdf+%E4%B8%AD%E6%96%87%2B-%3D

strpos

查找字符串首次出现的位置

<?php@see https://www.php.net/manual/zh/function.strpos.php
// strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
// 返回 needle 在 haystack 中首次出现的数字位置。$haystack = 'abc';
$needle = 'a';
var_dump(strpos($haystack, $needle));
// int(0)$haystack = 'abc';
$needle = 'b';
var_dump(strpos($haystack, $needle));
// int(1)$haystack = 'abc';
$needle = 'd';
var_dump(strpos($haystack, $needle));
// bool(false)

PHP中常用的十个字符串函数相关推荐

  1. 程序竞赛中常用的C字符串函数

    程序竞赛中常用的C语言字符串处理函数 前言: 最好是使用C++的std::string,但是有些情况下,比如字符串比较等,反而是C风格的字符串比较灵活.以下列举常用的函数. 字符串操作 char* s ...

  2. sqlserver中实现split分割字符串函数

    常用的. IF OBJECT_ID (N'fn_split') IS NOT NULL DROP FUNCTION  fn_split go CREATE  function dbo.fn_split ...

  3. Python中常用的高阶函数

    Python 中常用的高阶函数 ① filter(function,iterable)filter(function, iterable)filter(function,iterable)   过滤器 ...

  4. sql中截取字符串函数_SQL Server 2017中的顶级SQL字符串函数

    sql中截取字符串函数 SQL Server 2017 has been in the talk for its many features that simplify a developer's l ...

  5. MySql中常用的内置函数

    函数中可以将字段名当作变量来用,变量的值就是该列对应的所有值:在整理98在线字典数据时(http://zidian.98zw.com/),有这要一个需求,想从多音字duoyinzi字段值提取第一个拼音 ...

  6. MATLAB中常用到的绘图函数

    有关命令行环境的一些操作: (1) clc 擦去一页命令窗口,光标回屏幕左上角 (2) clear 从工作空间清除所有变量 (3) clf 清除图形窗口内容 命令1  figure 功能  创建一个新 ...

  7. php打开文件读写函数,php中常用文件操作读写函数介绍

    本文章介绍了下面几个常用的文件操作函数 file_get_contents 读取整个文件内容 fopen 创建和打开文件 fclose 关闭文件 fgets 读取文件一行内容 file_exists ...

  8. Python中常用最神秘的函数! lambda 函数深度总结!

    今天我们来学习 Python 中的 lambda 函数,并探讨使用它的优点和局限性 Let's do it! 什么是 Python 中的 Lambda 函数 lambda 函数是一个匿名函数(即,没有 ...

  9. MySQL—数仓ETL开发中常用到的日期函数

    在数据仓库ETL开发中,当以增量的方式进行数据同步时,会将数据表中的时间字段作为增量字段获取增量数据.对于MySQL数据库来说,总结了以下日期表示以及需要注意的问题. 一.需要注意的问题: 查看MyS ...

  10. Python中常用的内置函数

    通用函数 函数 功能描述 round(value, [n]) 以四舍五入法返回一个数值.value为需要四舍五入的数值:n为可选参数如果省略该参数则通过四舍五入返回整数,如果该参数没有省略则保留n位小 ...

最新文章

  1. 腾讯优图吴永坚:迈向深度学习,我们面临模型训练与推荐的双重考验
  2. 电脑课装b专用代码_FANUC伺服电机代码表(完整版)
  3. STM32 进阶教程 6 -  汇编与C混合编程
  4. windows 禁用ipv6服务_39.Dism++ Windows系统简洁优化
  5. Java国际化概念和使用介绍
  6. mysql配置环境变量,进阶加薪全靠它!
  7. MVC中单选按钮的实现
  8. 基于DWM1000的UWB测距调试(二)
  9. BI工具:cboard\superset 比较
  10. html设计学校网站,html学校网站
  11. HTML,CSS,JavaScript知识树思维导图
  12. 如何看懂公司的财务报表(1)
  13. 雅虎邮箱outlook设置 1 2 3
  14. 【Inpho精品教程】任务二:Inpho创建工程(创建项目、新建相机参数、导入照片、导入POS、生成航条、保存项目)
  15. 普罗米修斯prometheus
  16. [概率论]艾波寧捎信(poisson分布)
  17. 启动jupyter notebook链接不上内核 + 终端报错:Replacing stale connection
  18. matlab 扫雷,matlab 扫雷
  19. Hbase数据库中表的操作命令简介 Hbase shell命令
  20. SpringAOP所支持的AspectJ切点指示器

热门文章

  1. 【直播预告】相机模型与标定——Real world超级公开课
  2. csb反编译_GitHub - lyzz0612/csb2csd: cocostudio csb反编成csd
  3. pacman使用介绍
  4. 面试总结系列(一)------ 国际化中台事业部
  5. error: conflicting types for 错误原因及解决办法
  6. 如何设计SEO关键字分析统计表
  7. 【安装配置】DirectAdmin安装Nginx方法
  8. 接口邮件发送平台,定时发送邮件信息
  9. 马太效应 (两极分化现象)
  10. 如何在Python中异步操作数据库?