Gunn Team

Feed Rss

自制 php5.3函数lcfirst

09.24.2010, Default, by .

今天要写个转换model名称的函数,要把第一个字母转为小写,查PHP手册中有个lcfirst函数是要在PHP>=5.3.0才能用。
但是我服务器上的PHP版本是5.2.9,没法子自己制作一个吧! :)

function lcfirst($string = null) {
    if (!$string) return null;
    //dont't know how to do it with RegEx, :p
    return preg_replace('/(^[A-Z])/', strtolower(substr($string, 0, 1)), $string);
}

PS:写的不好! 好像效率不高 。 :(

自制 php5.3函数lcfirst 有 3 条回应

  1. 2010-12-24 在 14:52 范圣帅

    $string = strtolower(substr($string, 0, 1)) . substr($string, 1);

    回复
  2. 写的不错了。逻辑上很对头!! :)

    回复

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>