Welcome Guest, Not a member yet? Register   Sign In
Truncate String
#29

[eluser]sophistry[/eluser]
I wrote up a test file. it is at http://codeigniter.com/wiki/String_Parsing_Test_Code/.

conclusion:
------------------
substr() approach is best overall - it is fast with small strings (preg_replace is the fastest).
substr() stays fast no matter how large the input string gets (the other techniques slow with progressively longer strings).

here are the four approaches i tested:
Code:
function preg_rep_cut($in,$split_at_regex)
{
    return preg_replace($split_at_regex, '', $in, 1);
}

function preg_split_pre_cut($in,$split_at_regex)
{
    return current(preg_split($split_at_regex, $in, 2));
}

function substr_cut($in, $splitat)
{
    $out = (substr($in, 0, strpos($in, $splitat)));
    return $out?$out:$in;
}

function explode_cut($in, $splitat)
{
    return current(explode($splitat, $in, 2));
}
sample output from the code:
Code:
Array
(
    [/\<\|more\|\>.*+/si] => Array
        (
            [0] => 0.0064
            [10] => 0.0072
            [200] => 0.0086
            [400] => 0.0087
            [800] => 0.0091
            [1600] => 0.0385
            [3200] => 0.1111
        )

    [/\<\|more\|\>/s] => Array
        (
            [0] => 0.0066
            [10] => 0.0068
            [200] => 0.0095
            [400] => 0.0095
            [800] => 0.0098
            [1600] => 0.0105
            [3200] => 0.0755
        )

    [substr] => Array
        (
            [0] => 0.0071
            [10] => 0.0073
            [200] => 0.009
            [400] => 0.0089
            [800] => 0.009
            [1600] => 0.009
            [3200] => 0.009
        )

    [explode] => Array
        (
            [0] => 0.006
            [10] => 0.0064
            [200] => 0.0107
            [400] => 0.0132
            [800] => 0.0138
            [1600] => 0.0141
            [3200] => 0.0809
        )

)
Array
(
    [substr] => 0.00847142857143
    [/\<\|more\|\>/s] => 0.0183142857143
    [explode] => 0.0207285714286
    [/\<\|more\|\>.*+/si] => 0.0270857142857
)

enjoy. :coolsmile:


Messages In This Thread
Truncate String - by El Forum - 06-26-2008, 12:20 AM
Truncate String - by El Forum - 06-26-2008, 12:47 AM
Truncate String - by El Forum - 06-26-2008, 01:15 AM
Truncate String - by El Forum - 06-26-2008, 01:28 AM
Truncate String - by El Forum - 06-26-2008, 01:31 AM
Truncate String - by El Forum - 06-26-2008, 05:32 AM
Truncate String - by El Forum - 06-26-2008, 05:40 AM
Truncate String - by El Forum - 06-26-2008, 05:41 AM
Truncate String - by El Forum - 06-26-2008, 06:33 AM
Truncate String - by El Forum - 06-26-2008, 08:39 AM
Truncate String - by El Forum - 06-26-2008, 08:49 AM
Truncate String - by El Forum - 06-26-2008, 08:57 AM
Truncate String - by El Forum - 06-26-2008, 08:59 AM
Truncate String - by El Forum - 06-26-2008, 09:13 AM
Truncate String - by El Forum - 06-26-2008, 09:13 AM
Truncate String - by El Forum - 06-26-2008, 11:31 PM
Truncate String - by El Forum - 06-26-2008, 11:59 PM
Truncate String - by El Forum - 06-27-2008, 12:11 AM
Truncate String - by El Forum - 06-27-2008, 12:28 AM
Truncate String - by El Forum - 06-27-2008, 12:57 AM
Truncate String - by El Forum - 06-27-2008, 12:59 AM
Truncate String - by El Forum - 06-27-2008, 01:03 AM
Truncate String - by El Forum - 06-27-2008, 01:46 AM
Truncate String - by El Forum - 06-27-2008, 03:43 AM
Truncate String - by El Forum - 06-27-2008, 03:49 AM
Truncate String - by El Forum - 06-27-2008, 04:16 AM
Truncate String - by El Forum - 06-27-2008, 04:18 AM
Truncate String - by El Forum - 06-27-2008, 04:54 AM
Truncate String - by El Forum - 07-02-2008, 01:10 PM



Theme © iAndrew 2016 - Forum software by © MyBB