首页 > 学习 > 网络编程 > php > php字符串学习小结——php的语言基础(3)

php字符串学习小结——php的语言基础(3)

分享&收藏
时间:2010-09-20 16:57:54   
  [导读] c) 字符串
1. strlen函数 //计算字符串个数
*例:
<?php
$str = 'Hello World!';
$str2 = '你好世界!';
$str3 = 'The old lady was glad to be back at the block of flats where she lived. Her shoppin...
  [正文]

c) 字符串
1. strlen函数 //计算字符串个数

*例:
<?php
$str = 'Hello World!';
$str2 = '你好世界!';
$str3 = 'The old lady was glad to be back at the block of flats where she lived. Her shopping had tired her and her basket ad grown heavier with every step of the way home. In the life her thoughts were on lunch and a good rest .';

echo "\$str (strlen is ", strlen($str)."):<br>", $str, "<br><br>";
echo "\$str2 (strlen is ", strlen($str2)."):<br>", $str2, "<br><br>";
echo "\$str3 (strlen is ", strlen($str3)."):<br>", $str3, "<br><br>";

?>
2. 字符串的合并
<?php
$str1 = "Cao Peng";
$str2 = "video";
$str3 = "tutorials.";
$str4 = $str1 + $str2 + $str3; // 错误这是算术!!
$str5 = $str1.$str2.$str3; // 这才是concatenate your strings!
$str6 = $str1." ".$str2." ".$str3; // 适当使用空格!
echo "\$str5 (strlen is ", strlen($str5)."):<br>", $str5, "<br><br>";
echo "\$str6 (strlen is ", strlen($str6)."):<br>", $str6, "<br><br>";
?>

3.子字符串(一个字符串经过剪切后得到的字符串)
*例子
<?php
$str = "cao peng video tutorials.";

$part = substr($str, 1); //截取第一个字符开始到结束
echo $part, "<BR>";
$part = substr($str, 1, 5);//从第一个到结束
echo $part, "<BR>";
$part = substr($str, 0, 7);
echo $part, "<BR>";

echo $part, "<BR><BR>";

echo "\$str{4} = ", $str{4}, "<BR>"; //指定第四个字符
echo "\$str{10} = ", $str{10}, "<BR>";

?>
运行结果:cao peng video tutorials
ao pe
cao pen
p
v
 

返回本文所属专题php入门笔记php入门笔记专题

 

相关图文

    热门图文
    最新文章