zoukankan      html  css  js  c++  java
  • PHP中 字符串 常用函数

    //strpos    查找字符串中是否含有字符

    $str='abcde';
    $char='a';
     
    if(strpos($str,$char) !==false){
       echo '含有',$char, '
    ';
    }else{
     echo'不含有',$char,‘
    ’;
    }
     
     
     
    // str_replace 替换字符串
    $str='hi ,fuck';
    echo str_replace('fuck','f**k',$str),'
    ';
     
    显示 hi,f**k
     
     
    //strtr 替换一批字符串
    $str='男人,女人,男孩,女孩';
    echo strtr($str,array('男'=>'女',‘女’=>'男'));
     
    显示 女人,男人,女孩,男孩
     
    //substr 截取子字符串
    $str='tommrow is another day';
    echo substr($str,0,3);     //这里的0指截取的起点,3指截取的字符数。 从0开始截取3个字节。
    显示 tom
    echo substr($str,3,-3);  //第三个参数为负,代表结束位置从后往前。 
    显示 mrow is another
     
     
    //explode 拆分字符串
    $str='tech,linux,mysql';
    $arr=explode(','$str);   //把字符的,拆掉
    implode($arr,',')  //功能与explode相反。
     
     
    还有很多其他的字符串函数。需要经常练习
     
  • 相关阅读:
    第二周作业
    7-2 求最大值及其下标
    第十一周作业
    第九周编程总结
    第八周作业
    第七周作业
    第六周作业
    第五周作业
    第4周作业
    第三周作业
  • 原文地址:https://www.cnblogs.com/suiyuewuxin/p/5608507.html
Copyright © 2011-2022 走看看