TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   preg_split_callback (http://www.talkphp.com/general/2784-preg_split_callback.html)

Orc 05-08-2008 08:51 PM

preg_split_callback
 
I know this function doesnt exist, YET but I was wondering if it would be useful, or should we just rely on preg_replace_callback?

by the way, preg_split reminds me of explode :P its like exactly the same, doesnt it?

Wildhoney 05-09-2008 12:55 AM

Well, I'm not sure it's needed, to be honest. I have made us a simple example so you can have a play around. See for yourself if you think it's useful or not.

php Code:
function preg_split_callback($szPattern, $szSubject, $szCallback)
{
    if(!function_exists($szCallback))
    {
        return;
    }
   
    return $szCallback(preg_split($szPattern, $szSubject));
}

function split_last(array $aParts = array())
{
    foreach($aParts as &$pPart)
    {
        if(strpos($pPart, ';') === false)
        {
            continue;
        }
       
        list($pPart, $aParts[]) = explode(';', $pPart);
    }
   
    return $aParts;
}

$aParts = preg_split_callback('~\|~', 'a|b;c', 'split_last');

echo '<pre>';
print_r($aParts);
echo '</pre>';


All times are GMT. The time now is 02:32 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0