Pravin Ajaaz
Pravin Ajaaz

Reputation: 107

Get particular string using preg_replace in PHP

I have extracted doc comment block from a file which contains values like:

[0] => /**
        * Shows this block on only the listed pages.
        */
[1] => /**
        * Shows this block if the associated PHP code returns TRUE.
        */
[2] => /**
        * Implements hook_help().
        */
[3] => /**
        * Implements hook_theme().
        */

Now I need to extract all the texts which have "hook_*" after Implements .

So my final output should be an array with values like:

$hooks = array('hook_help', 'hook_theme');

What preg_replace expression should I use?

Upvotes: 0

Views: 59

Answers (1)

axxis
axxis

Reputation: 1004

Assuming that the array that holds the comments is $comments, the following will do:

$hooks = array();

foreach($comments as $comment) {
    if (preg_match("/Implements (hook_\w+)\(\)/", $comment, $matches))
        $hooks[] = $matches[1];
}

This will match text having the word Implements followed by hook_ and at least one letter followed by (). Then will store in $matches[1] the first parenthesized subpattern, i.e. hooks_...

Upvotes: 1

Related Questions