字符串数组php,回顾下PHP基础知识-字符串数组操作

59a63c988c15237648195f9c5a04a1e5.png

在日常工作中常常需要处理一些字符串或者数组,今天有时间来整理一下

//字符串截取

$str = 'Hello World!'

substr($str,0,5);//返回'Hello'

//中文字符串截取

$str = '你好,深圳';

$result = mb_substr($str,0,2);//返回你好

//查找字符串的首次出现

$emai = "[email protected]";

$domain = strstr($email,'@');//返回‘@163.com’

$domain = strstr($email, '@','true');//返回‘123’。

//查找字符串在另一字符串中第一次出现的位置。

$emai = "[email protected]";

strpos($emai,'@');//返回‘3'

//把字符串打散为数组

$str = '你好,深圳';

$arr = explode(',',$str);//返回值为array(2) { [0]=> string(6) "你好" [1]=> string(6) "深圳"

//字符串长度

$str = "Hello world";

$str1 = strlen($str);//返回11

//把字符串中的首字符转换为大写。

$str = "hello world";

$str1= ucfirst($str);//返回“Hello world”

//把字符串中的首字符转换为小写。

$str = "Hello world";

$str1= lcfirst($str);//返回“hello world”

//把字符串中每个单词的首字符转换为大写。

$str = "hello world";

$str1= ucwords($str);//返回“Hello World”

//反转字符串

$str = "hello world";

$str1= strrev($str);//返回“dlrow olleh”

//替换字符串中的一些字符

$str = "hello world";

$str1= str_replace('world','lisa',$str);//返回“hello lisa”

//字符串转换为大写

$str = "hello world";

$str1= strtoupper($str);//返回“HELLO WORLD”

//字符串转换为小写

$str = "HELLO WORLD";

$str1= strtolower($str);//返回“hello world”

?>

//数组整合成字符串

$arr = ['aa','bb','cc'];

$str = implode(',',$arr);//输出结果“aa,bb,cc”

//数组的key值

$arr = ['aa','bb','cc'];

$aa = array_keys($arr);//输出结果array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) }

//合并数组

$a1=array("a"=>"red","b"=>"green");

$a2=array("c"=>"blue","b"=>"yellow");

print_r(array_merge($a1,$a2));//返回array(4) { ["a"]=> string(5) "test1" [0]=> string(2) "bb" [1]=> string(2) "cc" [2]=> string(5) "test2" },注意当两个数组键值相同时,最后的会覆盖其他元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值