Jump to content

preg_replace() to preg_replace_callback


Dhillon

Recommended Posts

I need help converting this function.

This is the function-

function &fetch_full_ameinfo($findonly = false, $refresh = false)
{

    global $db, $vbulletin, $vbphrase, $stylevar;
    static $ameinfo = array();
    static $inied, $lastfind;

    if ($refresh)
    {

        $inied = false;

    }

    if ($lastfind && !$findonly)
    {

        $inied = false;
        $ameinfo = array();

    }

    if (!$inied)
    {

        if (!$refresh AND $vbulletin->options['automediaembed_cache'])
        {

            $path = $vbulletin->options['automediaembed_cache_path'];

            if (file_exists($path . "findonly.php"));
            {

                if ($findonly)
                {

                    include($path . "findonly.php");

                }
                else
                {

                    include($path . "ameinfo.php");

                }

                $inied = true;
                $lastfind = $findonly;

                return $ameinfo;
            }

        }

        if ($vbulletin->options['automediaembed_resolve'])
        {

            $embed = ",IF(extraction=1 AND embedregexp!= '', embedregexp, '') as embedregexp, IF(extraction=1 AND validation!= '', validation, '') as validation";
            $embedwhere = " AND ((extraction = 0 AND embedregexp = '') OR (extraction = 1)) ";

        }
        else
        {

            $embedwhere = " AND embedregexp = ''";

        }


        $sql = "SELECT findcode" . (!$findonly ? ", replacecode,title,container,ameid" : ",extraction$embed") . " FROM " . TABLE_PREFIX . "automediaembed WHERE status=1 $embedwhere
                        ORDER BY displayorder, title ASC";

        $results = $db->query_read_slave($sql);

        while ($result = $db->fetch_array($results))
        {

            if ($result['findcode'])
            {

                if (!$findonly)
                {

                    $ameinfo['find'][] = "~($result[findcode])~ie";
                    $ameinfo['replace'][] = 'ame_match_bbcode($param1, $param2, \'' . $result['ameid'] . '\', \'' . ame_slasher($result['title']) . '\', ' . $result['container'] . ', \'' . ame_slasher($result['replacecode']) . '\', \'\\1\', \'\\2\', \'\\3\', \'\\4\', \'\\5\', \'\\6\')';


                }
                else
                {

                    $ameinfo['find'][] = "~(\[url\]$result[findcode]\[/url\])~ie";
                    $ameinfo['find'][] = "~(\[url=\"?$result[findcode]\"?\](.*?)\[/url\])~ie";
                    $ameinfo['replace'][] = 'ame_match("\1", "", ' . intval($result['extraction']) .', "' . ($result['embedregexp'] ? "~" . ame_slasher($result['embedregexp']) . "~sim" : "") . '", "' . ($result['validation'] ? "~" . ame_slasher($result['validation']) . "~sim" : "") . '",$ameinfo)';
                    $ameinfo['replace'][] = 'ame_match("\1", "\2", ' . intval($result['extraction']) .', "' . ($result['embedregexp'] ? "~" . ame_slasher($result['embedregexp']) . "~sim" : "") . '", "' . ($result['validation'] ? "~" . ame_slasher($result['validation']) . "~sim" : "") . '", $ameinfo)';

                }

            }

        }

        $inied = true;
    }

    $lastfind = $findonly;

    return $ameinfo;
}

 

Called here-

function ame_process_bbcode(&$parser, &$param1, $param2 = '')
{
    if (class_exists('vB_BbCodeParser_Wysiwyg') AND is_a($parser, 'vB_BbCodeParser_Wysiwyg'))
    {
     return $text; 
    }
    else
    {
        global $vbulletin;
        ($hook = vBulletinHook::fetch_hook('automediaembed_parse_bbcode_start')) ? eval($hook) : false;
        $ameinfo = fetch_full_ameinfo();

        $text = preg_replace($ameinfo['find'], $ameinfo['replace'], ($param2 ? $param2 : $param1), 1);

        ($hook = vBulletinHook::fetch_hook('automediaembed_parse_bbcode_end')) ? eval($hook) : false;
        return $text;   
    }
}

 

full script: https://codeshare.io/5Dnk6Z

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.