first i want to say sorry
i have the following problem:
i am writing a php script that analyses other php files if there are class definitions inside. therefor i need a regex that matches all classes/namespaces
the regex has to be something like that
[syntax]#(class[\s]*(?<classname>[^\s\{]*)[\s]*\{(?<classcontent>.*?))#is
(namespace[\s]*(?<namespace>[^\s\{]*)[\s]*\{(?<namespacecontent>.*?))#is[/syntax]
the next given problem is that i use namespaces so i can have the class 'myClass' in several namespaces
therefor i changed all my scripts so they look like that
Code: Select all
<?php
declare(encode='whatever');
namespace a {
// some content
class myClass {
// something inside
}
}
namespace b {
// some other content
class myClass {
// something else inside
}
}
?>[syntax]
#^<\?(?:php)?[\s]*(?:declare[\s]*\([^\'\"]*(\'|\")(?:.*?)(?:\1)\)[\s]*)?(.*?)(?=[\s]*\?>)$#is
for explaination
^<\?(?:php)?[\s]* -> gets <?php and <? also whitespaces and linebreaks
(?:declare[\s]*\([^\'\"]*(\'|\")(?:.*?)(?:\1)\)[\s]*)? -> gets a declare(encoding=''); the only thing allowed when using namespaces
(.*?) - gets the rest until the php end flag
(?=[\s]*\?>)$ - gets whitespaces and linebreaks followed by ?>
[/syntax]
this regex works, but if i try to integrate something for my namespaces or classes i get no results when using prey_match_all.
my script stores all found classes/files to an array that is writen to a file so i can easily access this
Code: Select all
$variable = array(
[0] -> array (
['name'] -> 'myClass',
[0] -> array (
['namespace'] -> 'namespace a',
['file'] -> '/dir/sub/location_a'
),
[0] -> array (
['namespace'] -> 'namespace b',
['file'] -> '/dir/sub/location_b'
)
)
);
Code: Select all
array (
[0] -> '***file_get_contents()***'
[1] -> array (
['namespace'] -> 'namespace_a',
['classes'] -> array (
[0] -> 'myClass'
)
),
[2] -> array (
['namespace'] -> 'namespace_a',
['classes'] -> array (
[0] -> 'myClass'
)
)
)
is there a way to create a regex that splits my file_get_contents string so i get all classes (no matter if they are commented or defined inside a function or whatever) with the namespace they are inside ?