访问子字符串
strpos(string,find,start);//一个字符串在另一个字符串中首次出现的位置
提取子字符串
string substr(sting,start,len);//返回字符串的一部分
echo substr('Hello World!',6);//=====>World!
echo substr('Hello World!',6,5);//===>World
#如果start大于string的长度,substr返回false
#如果start为负值,substr会从string的结尾处开始反向推算
echo substr('Hello World!',-3);//=====>ld!
echo substr('Hello World!',-7,5);//===>Worl
#当start的负值超过字符串长度,则substr将start视为0
替换子字符串substr_replace(string,replacement,start,length);//把字符串的一部分替换为另一个字符串
echo substr_replace('Hello World!','SNK',-3);//===>Hello WorlSNK
按字或字节来反转字符串#按字节反转字符串
echo strrev('Hello Wolrd!');//====>!drloW olleH
#按字反转字符串
$s = "Tomorrow is anoter day";
$words = explode(' ',$s);
$words = array_reverse($words);
$s = implode(' ',$words);
echo $s;
#====>day anoter is Tomorrow
控制大小写#字符串首字母大写
echo ucfirst('hello world!');
#输出:Hello world!
#字符串每个单词首字母大写
echo ucwords('hello world!');
#输出:Hello World!
echo strtoupper('hello world!');
#输出:HELLO WORLD!
echo strtolower('HELLO WORLD!');
#输出:hello world!
删除字符串两端的空白符trim(string,charlist);//从字符串的两端删除空白字符和其他预定义字符
生成逗号分割的数据(Comma-separated values,CSV)#生成逗号分割数据
$a = array(
array('Junk','2013-12-23','2013-4-33',12.54),
array('Junk','2013-12-23','2013-4-33',12.54),
array('Junk','2013-12-23','2013-4-33',12.54),
array('Junk','2013-12-23','2013-4-33',12.54),
);
$f = fopen('a.csv','w') or die('can\'t open files');
foreach($a as $v){
if(!fputcsv($f,$v)){
die('Can\'t write in!');
}
}
fclose($f) or die('Close file error!');
#显示逗号分割数据
$a = array(
array('Junk','2013-12-23','2013-4-33',12.54),
array('Junk','2013-12-23','2013-4-33',12.54),
array('Junk','2013-12-23','2013-4-33',12.54),
array('Junk','2013-12-23','2013-4-33',12.54),
);
$f = fopen('php://output','w') or die('can\'t open files');
foreach($a as $v){
if(!fputcsv($f,$v)){
die('Can\'t write in!');
}
}
fclose($f) or die('Close file error!');
#将生成的csv放到字符串中$str = ob_get_contents();ob_end_clean();
解析CSV
$f = fopen('a.csv','r') or die('can\'t open file!');
echo "<table>\n";
while($csv_line = fgetcsv($f)){
echo '<tr>';
for($i=0,$j=count($csv_line);$i<$j;$i++){
echo '<td>'.htmlentities($csv_line[$i]).'</td>';
}
echo "</tr>\n";
}
echo '</table>';
fclose($f) or die('close file error');
分离字符串
explode(separator,string,limit);//把字符串分割为数组
是文本在固定长度处换行
#wordwrap默认以75个字符换行
$s = 'The PHP development team announces the immediate availability of PHP 5.4.19 and PHP 5.5.3. These releases fix a bug in the patch for CVE-2013-4248 in OpenSSL module and compile failure with ZTS enabled in PHP 5.4. All PHP users are encouraged to upgrade to either PHP 5.5.3 or PHP 5.4.19.';
echo "<pre>\n".wordwrap($s,50)."\n</pre>";