Blogger Information
Blog 36
fans 0
comment 0
visits 27928
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php字符串函数基础
小程_武汉_214945
Original
715 people have browsed it

heredoc、nowdoc

  1. <?php
  2. //heredoc功能与双引号类似,字符串内部不需要添加定界符
  3. $str=<<<heredoc
  4. this is "heredoc '
  5. heredoc;
  6. echo $str,'<br>';
  7. $a=<<<a
  8. 12345{$str}54321
  9. a;
  10. echo $a,'<br>';
  11. //nowdoc功能和单引号类似,字符串内部不需要添加定界符
  12. echo <<<nowdoc
  13. this is nowdoc\ '
  14. nowdoc;

字符串、数组的转换

  • imlpode,将数组转化为字符串, implode(‘分隔符’,\$arr),返回字符串

  • explode,字符串转化为数组 explode(‘分隔符’,\$arr,子串数),返回数组

  1. //implode,数组转为字符串
  2. $arr=['aaa','bbb','ccc'];
  3. echo implode('-',$arr);
  4. //explode字符串转化为数组
  5. $str='123-456-789';
  6. printf('<pre>%s</pre>',print_r(explode('-',$str),true));
  7. //将字符串分成两部分取出
  8. printf('<pre>%s</pre>',print_r(explode('-',$str,2),true));

字符串分割替换

  • 按位替换子串 substr_replace($arr,子串,$start,\$length),返回字符串或数组
  1. //替换字符串中的子串
  2. //从第四位替换三位
  3. echo substr_replace('123456789','abc',3,3);
  4. //替换字符串
  5. echo substr_replace('123456789','abc',0);
  6. //插入字符串
  7. echo substr_replace('123456789','abc',0,0);
  8. //替换最后一位
  9. echo substr_replace('123456789','abc',-1);
  10. //替换数组
  11. printf('<pre>%s</pre>',print_r(substr_replace(['a:1','b:2','c:3'],['a','b','c'],2,1),true));
  • 按子串内容替换 str_replace(查询,替换,$arr,$count 记录替换次数),查询和替换内容可以用数组表示,返回字符串
  1. //str_replace()
  2. echo str_replace('111','XXX','111222333222111',$count),"替换了{$count}次<br>";
  3. echo str_replace(['111','333'],'XXX','111222333222111',$count),"替换了{$count}次<br>";
  4. echo str_replace(['111','333'],['XXX','√√√'],'111222333222111',$count),"替换了{$count}次";
  • chunk_split($str,$number 每段字符数) 分割字符串
  1. echo var_export(chunk_split('123456789',2),true);

查询字符串

  • strstr(\$str,子串,\$before_search)查询字符串是否存在子串
  1. echo var_export(chunk_split('123456789',2),true),'<br>';
  2. //返回值为字符串中56前面的内容
  3. echo strstr('123456789','56',true),'<br>';
  4. //返回值为字符串56后面的内容
  5. echo strstr('123456789','56'),'<br>';
  6. //搜索不到子串时返回null
  7. var_dump(strstr('123456789','aaa')) ;
  • substr_count($arr,$子串)查询子串出现次数
  1. //查询子串12出现次数
  2. echo substr_count('123456543212','12');
  3. //从第二位开始查询
  4. echo substr_count('123456543212','12',1);
  5. //从最后两位查询
  6. echo substr_count('123456543212','12',-2,2);
  • strlen()查询字符串长度
  • trim()移除字符串两边的字符
  1. $str=' 123456';
  2. echo strlen($str);
  3. echo strlen(trim($str));
  4. echo strlen(rtrim($str,'56'));

总结:常用的字符串函数比较多,需要多加练习,以函数名,参数,返回值这三方面进行记忆

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:字符串函数是多, 归个类就不多了
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post