您的位置主页 > 编程专区 > Php > php大括号的妙用,php截取最后一个字符

php大括号的妙用,php截取最后一个字符

2009-10-30    文章来源:互联网    浏览次数:2144     分享文章
 
  1. // Get the first character of a string
  2. $str = 'This is a test.';
  3. $first = $str{0};  // $first = T;
  4. // Get the third character of a string
  5. $third = $str{2};  // $third = i;
  6.  
  7. // Get the last character of a string.
  8. $str = 'This is still a test.';
  9. $last = $str{strlen($str)-1};  // $last = t;
  10.  
  11. // Modify the last character of a string
  12. $str = 'Look at the sea';
  13. $str{strlen($str)-1} = 'e';  // $str = 'Look at the see';
 
  1. $a = "86-0754-88888888-";
  2. if ($a{strlen($a)-1} == "-")
  3.     $a = substr($a,0,strlen($a)-1);
  4. echo $a;

其中 $a{strlen($a)-1}只在PHP5.0以上可用。

文章评论(查看全部)

昵 称 *
电子邮箱 *
网 址      6 + 4 = ?