Welcome Guest, Not a member yet? Register   Sign In
Help documenting 3 new functions for CI 1.5.5
#11

[eluser]Derek Allard[/eluser]
Wow! Thanks aart-jan and Zacharias!

@BoltClock, I'd be very interested in seeing what you've found. Could you post sample code to show me, and also your proposed solution?
#12

[eluser]BoltClock[/eluser]
It's just a little oversight, really. When I looked in the helper (which was before I noticed this topic BTW), I saw this:

Code:
function reduce_multiples($str, $character = ',', $trim = FALSE)
{
    $str = preg_replace("#".$character."+#", $character, $str);

    if ($trim == TRUE)
    {
        $str = trim($str, $character);
    }
    
    return $str;
}

But it's a PCRE function so $character supplied as either the pound itself or metacharacters (()[]{}/^$.-+*) would make preg_replace() spit a warning, so I just escaped $character with preg_quote() and made the command only look for actually multiple instances to make life easier for it:

Code:
function reduce_multiples($str, $character = ',', $trim = FALSE)
{
    $preg_chars = preg_quote($character, '#') . preg_quote($character, '#');
    $str = preg_replace("#$preg_chars+#", $character, $str);

    if ($trim == TRUE)
    {
        $str = trim($str, $character);
    }
    
    return $str;
}

That should do it. For what it's worth, I did a quick test and found it at least 5 times faster too!
#13

[eluser]Derek Allard[/eluser]
Thanks all for your help and input. Your suggestions have been incorporated, as has the doc write up (thanks aart-jan!)
#14

[eluser]BravoAlpha[/eluser]
[quote author="BoltClock" date="1197068229"]
Code:
$preg_chars = preg_quote($character, '#') . preg_quote($character, '#');
[/quote]
Do you really need to quote the character twice?
Code:
function reduce_multiples($str, $character = ',', $trim = FALSE)
{
    $str = preg_replace('#'.preg_quote($character, '#').'{2,}#', $character, $str);

    if ($trim == TRUE)
    {
        $str = trim($str, $character);
    }
    
    return $str;
}
This seems to work just as well for me.
#15

[eluser]Majd Taby[/eluser]
Derek, while your eyes are on this thread (sorry for being off topic), under "Showing Errors Individually" in http://ellislab.com/codeigniter/user-gui...ation.html, I think there are missing code and pre tags.
#16

[eluser]Derek Allard[/eluser]
Thanks Zaatar. Already caught and fixed in the SVN... it seems like my whole life is spent in that thing Wink




Theme © iAndrew 2016 - Forum software by © MyBB