简体   繁体   中英

Regex to match namespaces in PHP

Today my objective is to retrieve all PHP class names associated with their namespace names but I got stuck. Here is an example of what I have:

$content =<<<END
<?php

namespace test;

class a { }

class b { }

namespace foo;

class bar { }
?>
END;

preg_match_all('~^\s*((?:namespace)\s+(\w+);)?\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', $content, $classes);
var_dump($classes);

The expression works only if there is at most one class in namespace but I can't figure out how to make it match all classes according to namespace.

You can try the following pattern:

((?:\\{1,2}\w+|\w+\\{1,2})(?:\w+\\{0,2})+)

For some reason, I had to match both Namespace\\Class and Namespace\\\\Class . But you have replace the {1,2} and {0,2} by respectively nothing and ? .

Test it online: https://regex101.com/r/fE2kU9/3

Perhaps it is easier to use the PHP tokenizer instead of a regular expression. Simply walk over the list of tokens looking for T_NAMESPACE and T_CLASS.

Example (untested):

$map = array();
$tokens = token_get_all($source_code);
$namespace = 'GLOBAL';
foreach ($tokens as $token) {
    if (!is_string($token)) {
        list($id, $text) = $token;
        if ($id == T_NAMESPACE) {
            $namespace = $text;
        }
        if ($id == T_CLASS) {
            $map[$namespace] = $text;
        }
    }
}
print_r($map);

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