PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Apr 21, 2019 6:06 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Wed Jan 05, 2005 7:50 am 
Offline
Forum Contributor

Joined: Tue Jul 27, 2004 6:19 am
Posts: 206
Hello people

I would like to run the following test on my PHP files.
I have 2 PHP functions called: iterate and display
here are the function signatures:
Syntax: [ Download ] [ Hide ]
public function iterate($param1, $param2, $param3 = '');

function display($param1, $param2, $param3 = '');

$param1, $param2 and $param3 are all strings
I would like to know exactly where I am using these functions in the code and what parameters are being used.

I would like to write a script that will iterate over all the PHP files (.php or .inc.php extension) in my projet and to use a regular expression to look for usage of these functions.
Whenever a usage is encountered, it should be written to a log file like so:
<$param1>\t<$param2>\t<path to file>\t<line #>\n

For example, if file a.php contains the code:
Syntax: [ Download ] [ Hide ]
echo display('test', 'Hello');

and file b.php contains the line:
Syntax: [ Download ] [ Hide ]
if ($obj->iterate('run', $val) == true)) {

The log file read:
Syntax: [ Download ] [ Hide ]
test   Hello   a.php   14
run    $val    b.php   20

where 14 is the line in a.php where the function was found and 20 is the line in b.php where the second function was found

I was wondering if someone could help me with this as my main problem is how to formulate the regular expression

regards


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 05, 2005 11:48 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
Syntax: [ Download ] [ Hide ]
<?php



        $text = 'if ($obj->iterate(''run'', $val) == true)) {

        if ($obj->iterate('
'run'' . ''test'', $val) == true)) {

        if ($obj->iterate('
'run'' . $not_work, $val) == true)) {';

       

        preg_match_all('#(iterate|display)\\s*\(((\\s*(([''"]).*?\\\\5)|\\$.*?)\\s*,){1,2}\\s*((([''"]).*?\\\\8)|\\$.*?)\\s*\\)#s', $text, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);

       

        var_export($matches);



?>
Syntax: [ Download ] [ Hide ]
array (
  0 =&amp;gt;
  array (
    0 =&amp;gt;
    array (
      0 =&amp;gt; 'iterate(''run'', $val)',
      1 =&amp;gt; 10,
    ),
    1 =&amp;gt;
    array (
      0 =&amp;gt; 'iterate',
      1 =&amp;gt; 10,
    ),
    2 =&amp;gt;
    array (
      0 =&amp;gt; '''run'',',
      1 =&amp;gt; 18,
    ),
    3 =&amp;gt;
    array (
      0 =&amp;gt; '''run''',
      1 =&amp;gt; 18,
    ),
    4 =&amp;gt;
    array (
      0 =&amp;gt; '''run''',
      1 =&amp;gt; 18,
    ),
    5 =&amp;gt;
    array (
      0 =&amp;gt; '''',
      1 =&amp;gt; 18,
    ),
    6 =&amp;gt;
    array (
      0 =&amp;gt; '$val',
      1 =&amp;gt; 25,
    ),
  ),
  1 =&amp;gt;
  array (
    0 =&amp;gt;
    array (
      0 =&amp;gt; 'iterate(''run'' . ''test'', $val)',
      1 =&amp;gt; 55,
    ),
    1 =&amp;gt;
    array (
      0 =&amp;gt; 'iterate',
      1 =&amp;gt; 55,
    ),
    2 =&amp;gt;
    array (
      0 =&amp;gt; '''run'' . ''test'',',
      1 =&amp;gt; 63,
    ),
    3 =&amp;gt;
    array (
      0 =&amp;gt; '''run'' . ''test''',
      1 =&amp;gt; 63,
    ),
    4 =&amp;gt;
    array (
      0 =&amp;gt; '''run'' . ''test''',
      1 =&amp;gt; 63,
    ),
    5 =&amp;gt;
    array (
      0 =&amp;gt; '''',
      1 =&amp;gt; 63,
    ),
    6 =&amp;gt;
    array (
      0 =&amp;gt; '$val',
      1 =&amp;gt; 79,
    ),
  ),
)
note how the third line isn't found, due to the more complex nature of the expression involved and my lack of time to fiddle with it more.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 05, 2005 12:07 pm 
Offline
DevNet Master

Joined: Tue Jan 20, 2004 12:11 am
Posts: 4897
Location: Leuven, Belgium
your question has already been answered, but you might want to check out this project: http://ctags.sourceforge.net too....

if i understand a little, it does (much) more than what you asked for :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 05, 2005 6:32 pm 
Offline
Forum Contributor

Joined: Tue Jul 27, 2004 6:19 am
Posts: 206
thanks feyd

could you give me a short explanation of this complex regular expression?
I am very new to regular expressions and I tried to understand how you solved it, but I am lost


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 05, 2005 6:43 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
basics:
(iterate|display) = look for iterate or display
\s*\( any amount of whitespace followed by an opening paren.
([''"]).*?\\5)|\$.*? look for a quoted string symboling or a vague description of a variable.
{1,2} expect 1 to 2 of them
([''"]).*?\\8)|\$.*? look for a quoted string symboling or a vague description of a variable.
\s*\) any amount of whitespace followed by the closing paren of the function call.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 05, 2005 6:47 pm 
Offline
Forum Contributor

Joined: Tue Jul 27, 2004 6:19 am
Posts: 206
I have 2 questions:
what do the numbers 5 and 8 mean in ([''"]).*?\\5)|\$.*? and ([''"]).*?\\8)|\$.*?

and why do you precede them with \\

I notice that you surround your expression with #. Is it custom?
I also saw expressions that are surrounded by '
is there a difference?
do I must surround my regular expressions with # ?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 05, 2005 9:37 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
you have to surround your pattern with a symbol character... any will work. However, some characters are metacharacters for patterns. Although you can use them for patterns, it's often suggested to find a symbol you like that isn't one of them. ^+-*()[]{}=?|!$ are all metacharacters in some form or another. /@# are the most often used that I've seen as pattern start and end markers (surrounding a pattern)

the 5 and 8 are back references to marked segments of the pattern. In both cases they are [''"], but from differing positions. \\5 references the 5th subpattern that's marked for remembering.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Exabot [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group