zoukankan      html  css  js  c++  java
  • Php学习之路三(字符串操作)

    <?php
    /*String 去除空格@author=lixingle
     * string trim(string $str [,string $charlist);
     * ltrim(string $str [,string $charlist);//取出做空格
     * rtrim(string $str [,string $charlist)  取出右空格
     * */
    
    	$str1="\t\t   myphpa  ";
    	echo var_export($str1);
    	echo var_export(trim($str1));//取出空格
    echo "<br>";
    
    ?>
    
    <?php 
    /*字符串比较函数
     * strcmp(string $str1,string $str2)区分大小写的比较
     * strcasecmp(string $str1,string $str2)不区分大小写的比较
     * strncasecmp(string $str1,string $str2,int leng)用于不区分大小写的字符串的比较
     * strncmp(string $str1,string $str2,int leng)用于字符串选择性的比较,区分大小写
     * 第三个参数为要比较的长度
     * 
     * */
    echo strcmp("abcD","abcd")."<br>";//返回-1因为小写字母的ASCII值大于大写祖母的值
    echo strcasecmp("abcD","abcd")."<br>";//返回0  
    
    ?>
    <?php 
    /*字符串的查找和匹配
     * String strstr(string $haystack,string $needle);
     * $haystack为母字符串。$needle为查找字符串.
     * String strrchr(string $haystack,string $needle);
     *  $haystack为母字符串。$needle为查找字符串.
     *  
     *  区别:两个函数的区别是strstr();用来查找第一次出现的位置,返回从此次为止到结束的字符串;
     *  			   strrchr()用来查找最后出现的位置,返回从此次为止到结束的字符串;
     * */
    	$needle="changchao";
    	$parent="I love changchao,heihei,changchao,hehe!!!";
    	
    	echo strrchr($parent,$needle)."<br>";
    	echo strstr($parent,$needle)."<br>";
    	
    	$text="This is a test";
    	$needle="is";
    	echo strlen($text)."<br>";
    	echo substr_count($text, $needle)."<br>";//返回结果为2
    	echo substr_count($text, $needle,3)."<br>";//从第四个字符开始查找。返回结果为1
    	echo substr_count($text, $needle,3,3)."<br>";//从第四个字符开始查找。查找长度为3.返回结果为0
    /*
     *Int strrpos(string $haystack,mixed $needle,int len); //查找最后一次的位置,
     *														$needle如果是字符串只取第一个字符
     * int strpos(string $haystack,string $needle,int len); //查找第一次的位置
     * 
     * string str_replace(string $str1,string $str2,string $str3);
     * $str1为要被替换的字符串。$str2为新字符串,$str3为原字符串
     * string substr_replace(string $str1,string $str2,string $str3,int n);
     * $str1为要被替换的字符串。$str2为新字符串,$str3为原字符串,n为替换长度
     * */
    	echo str_replace("World","PHP","Hello World!")."<br>";//输出Hello PHP!
    	
    	
    ?>
    <?php 
    /*分割字符串:
     * Array explode(string $separator,string $string,[,int $limit]);
     * $separator设置一个分隔符号,不能为空。$string为所操作的字符串,$limit为将字符串分割为多少个字符串
     * 
     * */
    	$date="localhost/root/123456";
    	list($host,$user,$password)=explode("/",$date);//$date分割成含三个变量的数组
    	echo "\$host=".$host."<br>";
    	echo "\$user=".$user."<br>";
    	echo "\$password=".$password."<br>";
    ?>
    

  • 相关阅读:
    VSCODE打开一个文件,另一个文件就关闭的问题的解决方法
    elementui的el-tree第一次加载无法展开和选中的问题
    Java线程知识:二、锁的简单使用
    “商家参数格式有误”应用切微信H5支付完美解决方案
    git 基础操作,公私钥认证/ssh公私钥登录
    Python数据分析之亚马逊股价
    Python分析6000家破产IT公司
    Python数据分析之股票数据
    Python数据分析之全球人口数据
    Vue 面试重点真题演练
  • 原文地址:https://www.cnblogs.com/lixingle/p/3313012.html
Copyright © 2011-2022 走看看