PHP: Get function argument with preg_match_all

Question:

Good afternoon:

I'm trying to get the value of calls to a particular PHP function straight from the source code.

What I need is to scan each of the lines of source code, and if there are one or more altext() function calls, get the argument passed. For example:

<img alt="<?php echo altext('[Este é um texto alternativo]');?>">

In the case of the example, I only need to be returned "This is alternative text" (without quotes). The [] were placed to prevent that, if there is a ') in the middle of the text, the search stops before the end. I'm trying to use preg_match_all("/\[altext\]\(\'\[(.*)\]\'\)/", $data, $matches); but it's not returning anything. $data is the line of code I fgets source code with fgets .

Answer:

I got it from reading a text by Martin Streicher and the PHP Manual

preg_match_all("/altext\(\'(.*)\'\)/", $data, $matches);

And it still doesn't require any other tricks if a ') appears in the middle of the argument.

I can't believe it was that easy. I looked for days.

Scroll to Top