Welcome Guest, Not a member yet? Register   Sign In
Little changes for ./system/helpers/string_helper.php
#1

[eluser]manuw[/eluser]
Dear,

I have added two small new features to ./system/helpers/string_helper.php.

Code:
/**
    * check if haystack string begins with needle
    *
    * @param string $haystack the string to check
    * @param string $needle string it should begin with
    * @return bool true if begins with
    * @assert ('hello world', 'hello') === true
    * @assert ('hello world', 'world') === false
    * @assert ('ello world', 'hello') === false
    * @assert ('hello', 'hello') === true
    * @assert ('hell', 'hello') === false
    * @assert ('hell', '') === true
    */
if ( ! function_exists('beginsWith'))
{
    function beginsWith($haystack, $needle)
    {
        return substr($haystack, 0, strlen($needle))===$needle;
    }
}

and

Code:
/**
    * check if haystack string ends with needle
    *
    * @param string $haystack the string to check
    * @param string $needle string it should end with
    * @return bool true if ends with
    * @assert ('hello world', 'world') === true
    * @assert ('hello world', 'hello') === false
    * @assert ('hello orld', 'world') === false
    * @assert ('hello', 'hello') === true
    * @assert ('hell', 'ello') === false
    * @assert ('hell', '') === true
    */
if ( ! function_exists('endsWith'))
{
    function endsWith($haystack, $needle)
    {
        $len=strlen($needle);
        if (0==$len) {
             return true;
        }
        return substr($haystack, -1*$len)===$needle;
    }
}

I did not want to keep to myself Wink
The code should be self explanatory

Greetz, manuw
#2

[eluser]n0xie[/eluser]
Would be easier to just extend the helper instead of hacking a system file, but other than that, nice contribution.
#3

[eluser]hugle[/eluser]
Nice contribution Smile thanks

btw, you can name it MY_string_helper.php
put it in app/helpers

and when you'll load it by load->helper('string');

you'll get all core string functions and your added too Smile

so if you update someday your core, you'll won't loose your functions!

good luck Smile
#4

[eluser]sophistry[/eluser]
lovely!

how about this to make it streamlined (saves a var assign):
Code:
function endsWith($haystack, $needle)
    {
        if ($needle==='') return true;
        return substr($haystack, -1*strlen($needle))===$needle;
    }
#5

[eluser]manuw[/eluser]
Thanks Guys.

Yes. A own helper are better way =)
I have a own CodeIgniter "Remix" with many function in the core (e.g. lib, helper).
I add it next time.

Edit:
@sophistry: your "bonsai" looks better then mine Sad

Code looks good, but i dont like this coding style
Code:
if(x===y) return true;

I'm a friend of the { } Wink. But okay, each his own.

Greetz
#6

[eluser]manuw[/eluser]
The next one.
Code:
/**
* split passed string to strings of passed length
*
* @param string $str
* @param int $len
* @return array
* @assert ('aabbccd', 2) == array('aa', 'bb', 'cc', 'd')
* @assert ('aabbccd', 3) == array('aab', 'bcc', 'd')
*/
if (!function_exists('splitToLength')){
    function splitToLength($str, $len) {
        $res=array();
        $slen=strlen($str);
        for ($spos=0; $spos<=$slen; $spos+=$len) {
            $res[]=substr($str, $spos, $len);
        }
        return $res;
    }
}

Github

greetz, manuw
#7

[eluser]danmontgomery[/eluser]
http://www.php.net/manual/en/function.str-split.php
#8

[eluser]sophistry[/eluser]
str_split() is PHP5. i found this funny little thing at the php.net comments page for str_split(). it's so cute!
Code:
function strsplit($str, $l=1)
    {
        do
        {
            $ret[]=substr($str,0,$l);
            $str=substr($str,$l);
        }
        while($str != "");
        return $ret;
    }




Theme © iAndrew 2016 - Forum software by © MyBB