zoukankan      html  css  js  c++  java
  • [Yii Framework] A component of cutting string

    1. Create a file named "StringHelper.php", and locate it in protected/components , and insert the code as below:

    代码
    <?php
    /**
    */
    class StringHelper extends CApplicationComponent
    {
    public function substr($string, $start = 0, $length = 0, $append = '...')
    {
    $stringLast = "";
    if ($start < 0 || $length < 0 || strlen($string) <= $length)
    {
    $stringLast = $string;
    }
    else
    {
    $i = 0;
    while ($i < $length)
    {
    $stringTMP = substr($string, $i, 1);
    if ( ord($stringTMP) >=224 )
    {
    $stringTMP = substr($string, $i, 3);
    $i = $i + 3;
    }
    elseif( ord($stringTMP) >=192 )
    {
    $stringTMP = substr($string, $i, 2);
    $i = $i + 2;
    }
    else
    {
    $i = $i + 1;
    }
    $stringLast[] = $stringTMP;
    }
    $stringLast = implode("",$stringLast);
    if(!empty($append))
    {
    $stringLast .= $append;
    }
    }
    return $stringLast;
    }
    }




    2. Demo

    Yii::import("application.components.StringHelper");
    $stringHelper = new StringHelper;
    echo $stringHelper->substr('This is a test string!', 0, 8);





    Have fun with Yii! :)

  • 相关阅读:
    os模块
    函数练习
    集合 去重
    作业二:购物车程序
    作业一: 三级菜单
    字典练习
    字典
    切片
    冒泡练习
    判断整型数据奇偶数
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/1806853.html
Copyright © 2011-2022 走看看