简体   繁体   中英

Regex to match specific function in php file

I have been busting my head on this one for some time. Final scope: parse contents of php files (opened as text files) and get the first parameter of a function t().

Here is what I have so far:

<\?[(?:php)]{1}.*?t\(["'](.*?)["']\s*,*.*?\).*[\?>]*

For the following content, it should return "Test 1" through Test 18.

Here is the text (I am aware that there are syntax errors).

<?php t('Test 1') ?>
<?php t("Test 2") ?>
<?php= t("This should fail") ?>
<?php = t("This should fail") ?>
<?php =t("This should fail") ?>
<?=t("Test 3")?>
<?=
t("Test 4") ?>
<?= $vrum+$vrum;t('Test 5')?>
<?= t('Test 6') ?>
<?=t("Test 7",$a)?>
<?=t("Test 8 %s, %d",$b,$a)?>
<?=t("Test 9 %s, %d", $b, $a)?>
<?php echo t("Test 10");?>
<?php echot("This should fail");?>
<?phpecho t("This should fail");?>
<?php echo t('Test 11');?>
<?php echo t('Test 12 %s\'%d',$a , $b);?>
<?php echo t('Test 13 %s\'%d\'',$a , $b);?>
<?php echo t('Test 14 %s\'%d',t('Test 15') , $b);?>
<?php echo t('Test 16 %s\'%d', t('Test 17') , $b);?>
<?php echo T("This should fail");?>
<?php echo t("Test 18");

I'm having problems with what goes before the function, as it needs to be a valid php tag

<?php (followed by a space) or <?= (with or without space)

Can someone please point me in the right direction?

Thanks

You don't need to parse valid php code with regular expressions, since there is a built in tokenizer available:

$tokens = token_get_all($text);

foreach ($tokens as $i => $token) {
    if ($token[0] == T_STRING && isset($tokens[$i + 1])) {
        $next = $tokens[$i + 1];

        if (is_string($next) && $next == '(' && isset($tokens[$i + 2])) {
            $arg = $tokens[$i + 2];

            var_dump($arg[1]);
        }
    }
}

Demo: http://ideone.com/jovM7R

A note: it does not do exactly what you asked but it's trivial to add it, so use it as a base for a proper solution

References:

Like that:

<\?(?:php(?:\s+echo+)?\s+|=[^=]*?\s*)t\(["'](.*?[^\\])["'].*?(?:t\(["'](.*?[^\\])["']\).*?)*\).*[\?>]*

Test HERE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM