码迷,mamicode.com
首页 > Web开发 > 详细

PHP字符串 集合的相关函数

时间:2016-03-30 22:12:56      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:

PHP第三次字符串处理函数
<?php
/**
* Created by PhpStorm.
* User: yangguojun
* Date: 16/3/16
* Time: 下午7:26
*/
//1.去掉空格及特殊字符 trim函数

//$str=‘asdf ghjkl\t‘;

//echo($str);
//trim 去掉两端空格

//echo(trim($str));
//echo(‘test‘);

//移除两端指定的字符 as 和 \t
//echo(trim($str,‘as\t‘));

//$str1=‘ asdfg‘;
//移除左边的空格
//echo(ltrim($str1));

//$str2=‘lamco.com.cn‘;
//
//echo(ltrim($str2,‘lam‘));

//$str3=‘www.lamco.com.cn‘;
//移除右边空格
//echo(rtrim($str3));
//echo(rtrim($str3,‘.cn‘));

//2.获取字符串长度

//$str3=‘hello lamco‘;
//echo(strlen($str3));

//3.截取字符串
//$str4=‘http://www.lamco.com.cn‘;

//echo(substr($str4,0,4));
//从指定索引位置开始截取字符串 到字符串结尾
//echo(substr($str4,7));

//echo(substr($str4,-5));

//从字符串的末尾开始算起,截取的倒数第二位位置
//echo(substr($str4,-5,-2));


//4.比较字符串

//$str1=‘lisi1‘;

//$str2 = ‘lisi‘;

//-1 0 1

//echo(strcmp($str1,$str2));

//$str1=‘10‘;
//$str2=‘2‘;
//按照字母的出现顺序比较 不区分大小写
//echo(strcasecmp($str1,$str2));

//按照自然数大小比较
//echo(strnatcmp($str1,$str2));

//$str3 =‘asdff‘;
//$str4=‘asdfghj‘;

//比较前几个字符
//echo(strncmp($str3,$str4,3));

//查找字符串

//$str = ‘http://www.welcome to lamco.com.cn‘;
//返回复合条件的字符串到结尾的所有字符
//echo(strstr($str,‘www‘));

//$str =‘asdfghgfdsadasdf‘;
//检索指定字符串在字符串中出现的次数
//echo(substr_count($str,‘a‘));

//$str=‘welcome to sdzysdzysdzy‘;

//查找指定字符串中需要替换的字符,
//1.要替换的字符
//2.替换后的字符
//3.要查询的字符串
//4.替换字符的次数

//echo(str_ireplace(‘sdzy‘,‘lamco.com.cn‘,$str,$count));
//echo($count);

//替换字符串

//$str=‘welcome to lamco.com.cn‘;
//从指定索引位置替换指定长度的字符串
//echo(substr_replace($str,‘lamco‘,0,7));

//字符串格式化
//echo(number_format(123456));

//echo(number_format(123456,3));

//echo(number_format(123456,3,‘,‘,‘.‘));

//分割字符串

//$str =‘a+s+d+f+g+h+k‘;
//
//$arr= explode(‘+‘,$str);
//
//foreach($arr as $item)
//{
// echo($item.‘ ‘);
//}

//合成字符串
//$newStr= implode(‘_‘,$arr);
//echo($newStr);

 

<?php
/*
$arr=array(‘iOS‘,‘android‘,‘wp‘);

foreach($arr as $subject)
{
echo($subject.‘ ‘);
}

for($i=0;$i<count($arr);$i++)
{
echo($arr[$i].‘ ‘);
}
*/

//关联数组
/*
$arr=array(‘name‘=>‘lisi‘,‘age‘=>‘23‘,‘addr‘=>‘bj‘);

foreach($arr as $key=>$value)
{
echo($key.‘ ‘);
}
foreach($arr as $key=>$value)
{
echo($value.‘ ‘);
}

$language=array("ASP","PHP","JSP");
while((list($key,$value)=each($language))){
echo $key."=>".$value;
echo "<br/>";
}
*/

//数组处理函数

$language=array("ASP","PHP","JSP");
//返回集合的所有value值
$arrValue = array_values($language);

//foreach($arrValue as $value)
//{
// echo($value.‘ ‘);
//}

$arr=array(‘name‘=>‘lisi‘,‘age‘=>‘23‘,‘addr‘=>‘bj‘);

$arrKey=array_keys($arr);

//foreach($arrKey as $k)
//{
// echo($k.‘ ‘);
//}
//返回集合中是否存在指定元素
//echo(‘num= ‘. in_array(‘ASP‘,$language,true));

//交互集合在键和值
//$arrChange=array_flip($arr);
//foreach($arrChange as $k=>$v)
//{
// echo( $k.‘ ‘);
//}
//翻转集合
//$arrReverse=array_reverse($language);
//foreach($arrReverse as $v)
//{
// echo($v.‘ ‘);
//}

//统计集合中元素的个数
//echo(count($arr));
//统计集合中所有值出现的次数

//$arr1=array(‘name‘=>‘lisi‘,‘age‘=>‘23‘,‘age1‘=>‘23‘,‘addr‘=>‘bj‘);
//
//$allValues = array_count_values($arr1);
//foreach($allValues as $a)
//{
// echo($a.‘ ‘);
//}

$arr2=array(‘a‘,‘b‘,‘t‘,‘t‘,‘t‘,‘r‘);
//去掉重复项
//$finarr=array_unique($arr2);
//
//foreach($finarr as $a)
//{
// echo($a . ‘ ‘);
//}

//集合排序

// rsort($arr2);
//
//foreach($arr2 as $v)
//{
// echo($v);
//}

//合并数组

$arr11 =array(‘aa‘,‘bb‘);
$arr22 = array(‘cc‘,‘dd‘,‘bb‘,‘aa‘);
$arr33=array_merge($arr11,$arr22);
foreach($arr33 as $v)
{
echo($v.‘ ‘);
}
echo(‘oookkk‘);
//数组交集
$arr44=array_intersect($arr11,$arr22);
foreach($arr44 as $v)
{
echo($v.‘ ‘);
}
echo(‘oookkk‘);
//返回集合的差
$arr55=array_diff($arr22,$arr11);

print_r($arr55);
foreach($arr55 as $v)
{
echo($v.‘ ‘);
}

$num =array_rand($arr22);

print_r($arr22[$num]);

$arr77=array(‘a‘,‘b‘,‘c‘,‘d‘,‘e‘);

shuffle($arr77);
print_r($arr77);

echo( floor(12.8));

echo(rand());

PHP字符串 集合的相关函数

标签:

原文地址:http://www.cnblogs.com/tianlianghong/p/5339083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!