0

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

2 Answers 2

1

Like that:

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

Test HERE

Sign up to request clarification or add additional context in comments.

Comments

1

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:

1 Comment

Thanks. This is another way of doing it, but I would then need to parse result, and it still catches invalid tags. However, it's a nice method of parsing php. This may be useful in other circumstances.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.