classic example1

 

<?php
//1.连接一个heredoc定义的字符串
$html = <<<END
<div class = "divClass">
<ul class = "ulClass">
<li>
END
."the list item"."</li></div>";

echo $html;

 

结果:   the list item

 

//2.用strpos()来查找字符串,strpos必须用===或者!==,因为如果找到字符串,会返回开始处位置,值为0.
$email = "cccccc#cc.cc";
if(strpos($email,'@') === false){
echo 'there was no @ in the e-mail address!';
}

结果: there was no @ in the e-mail address!

 

//3.用substr()来提取一个子字符串
echo "<br/>";
$string = 'watch out for that tree';
$start = -17;
$length = 5;
$substring = substr($string,$start,$length);
echo $substring;

结果:out f

 

//4.用substr_replace()替换自字符串
//把从位置$start开始到$sold_string结尾处的所有字符
//替换成$new_substing
//$new_string = substr_replace($old_string,$new_substring,$statr);
echo "<br/>";
echo substr_replace('my pet is a blue dog.','fish','12');

//把从$start位置开始的$length个字符替换成$new_substring
//$new_srting = substr_replace($old_string,$new_substring,$start,$length);
echo "<br/>";
echo substr_replace('my pet is a blue dog.','green','12',4);

结果:  my pet is a fish

my pet is a green dog.

 

//5.处理字符串中每一个字节

echo "<br/>";
$string = "this weekend,I'm going shoping for a pet chicken";
$vowels = 0;
for ($i = 0,$j = strlen($string);$i < $j; $i++){
if(strstr('aeiouAEIOU',$string[$i])){
$vowels++;
}
}
echo $vowels;

结果: 14

 

//6."Look and say"序列
echo "<br/>";
function lookandsay($s){
//将保存返回值的变量初始化为空字符串
$r = '';
//$m用于保存我们要查找的字符
//同时将其初始化为字符串中的第一个字符串
$m = $s[0];

//$n用于保存我们找到的$m的数目,将其初始化为1
$n = 1;
for($i = 1;$j = strlen($s),$i < $j;$i++){
//如果这个字符与上一个字符相同
if($s[$i] == $m){
//这个字符的数目加1
$n++;
}else{
//否则,把数目和这个字符追加到返回值
$r .=$n.$m;
//把要找的字符串设置为当前的字符
$m = $s[$i];
//并把数目重置为1
$n = 1;
}
}
//返回构建好的字符串以及最终的数目和字符
return $r.$n.$m;
}
for($i = 0,$s = 1;$i < 10; $i++){
$s = lookandsay($s);
echo "$s <br/>\n";
}

结果:


11 
21 
1211 
111221 
312211 
13112221 
1113213211 
31131211131221 
13211311123113112211 

//7.按字反转字符串
echo "<br/>";
$s = "Once upon a time there was a turtle.";
$words = explode(' ',$s);
$words = array_reverse($words);
$s = implode(' ',$words);
echo $s;

结果: turtle. a was there time a upon Once

 

//8.简化后按字翻转字符串的代码
echo "<br/>";
$z = "this is a dog.";
$reversed_s = implode(' ',array_reverse(explode(' ',$z)));
echo $reversed_s;

 

结果: dog. a is this

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值