• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Php学习之路3(字符串操作)

php 搞代码 3年前 (2022-01-24) 10次浏览 已收录 0个评论

Php学习之路三(字符串操作)

<?php/*String [email protected]=li<em>¥本文来%源[email protected]搞@^&代*@码)网5</em><strong>搞gaodaima代码</strong>xingle * 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>";?>


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Php学习之路3(字符串操作)
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址