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

php字符串处理

时间:2018-06-02 22:54:26      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:第一个   \n   pytho   off   replace   image   muti   字符串   out   

去除空格  trim($被处理的字符串,$要去除的字符串)   ltrim  rtrim
<?php $ss="d i am a tesad"; $s=trim($ss,‘d‘); echo $s; ?>
对应python:a.lstrip() ,a.rstrip(),a.strip(),a.strip("acc")


在字符串中插入字符 chunk_split($被处理的字符串,间隔,插入的字符串)

<?php
echo chunk_split("iamasmallgogo",2,",");        ia,ma,sm,al,lg,og,o,
echo chunk_split("iamasmallgogo",1,",");        i,a,m,a,s,m,a,l,l,g,o,g,o,
?>

数字符串中各个字符出现的频率 count_chars($被处理的字符串,模式)

技术分享图片

<?php
$data="i have some ii habasome";
var_dump(count_chars($data,1));
var_dump(count_chars($data,3));
?>

array(10) {
[32]=>int(4)
[97]=>int(3)
[98]=>int(1)
[101]=>int(3)
[104]=>int(2)
[105]=>int(3)
[109]=>int(2)
[111]=>int(2)
[115]=>int(2)
[118]=>int(1)
}
string(10) " abehimosv"

将字符串按分隔符打成数组 array explode ( string $delimiter , string $string [, int $limit ] )

<?php
$data="i have some ii habasome";
var_dump(explode(" ",$data));
var_dump(explode(" ",$data,2));
?>

array(5) {
[0]=>
string(1) "i"
[1]=>
string(4) "have"
[2]=>
string(4) "some"
[3]=>
string(2) "ii"
[4]=>
string(8) "habasome"
}
array(2) {
[0]=>
string(1) "i"
[1]=>
string(21) "have some ii habasome"
}

 将数组按分隔符链接成字符串  

string implode ( string $glue , array $pieces )
string implode ( array $pieces )                别名join

<?php

$array = array(‘lastname‘, ‘email‘, ‘phone‘);
$comma_separated = implode(",", $array);

echo $comma_separated; // lastname,email,phone

?>

string lcfirst ( string $str )使一个字符串的第一个字符小写

string md5 ( string $str [, bool $raw_output = false ] )计算字符串的 MD5 散列值

string nl2br ( string $string [, bool $is_xhtml = TRUE ] )在字符串所有新行之前插入 HTML 换行标记

string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] ) 填充字符串到指定长度

string str_repeat ( string $input , int $multiplier )   将$input重复$mutiplier次

str_replace  替换字符串

<?php
$s="我是Jack,我喜欢cb,我从来都很nice";
$c=2;
echo str_replace(‘jack‘,‘tom‘,$s)."\n";
echo str_replace([‘Jack‘,‘cb‘,‘nice‘],[‘tom‘,‘rb‘,‘bad‘],$s)."\n";
?>

我是Jack,我喜欢cb,我从来都很nice
我是tom,我喜欢rb,我从来都很bad
string str_shuffle ( string $str )

<?php

$str = "Hello Friend";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

print_r($arr1);
print_r($arr2);

?>

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
)

Array
(
    [0] => Hel
    [1] => lo
    [2] => Fri
    [3] => end
)
int stripos ( string $haystack , string $needle [, int $offset = 0 ] )

返回在字符串 haystackneedle 首次出现的数字位置。 

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

返回 needlehaystack 中首次出现的数字位置。 

string strrev ( string $string )

返回 string 反转后的字符串。 

 

;;;;;;;;;不想写了。。。。。。。

php字符串处理

标签:第一个   \n   pytho   off   replace   image   muti   字符串   out   

原文地址:https://www.cnblogs.com/webdev8888/p/9127178.html

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