I have a simple regex which checks an entire string for a function declaration. So in this code:
public function Test($name)
{
echo 'In test';
}
It will find the first part:
function Test($name)
{
And it replaces that with a custom piece:
function Test($name)
{
echo 'New piece';
Which eventually makes my code look like this:
public function Test($name)
{
echo 'New piece';
echo 'In test';
}
This all works perfectly fine with this regex:
preg_match_all ( '/function(.*?)\{/s', $source, $matches )
The problem is, is that i want to ignore everything when the regex sees a script tag. So in this case, this source:
public function Test($name) //<--- Match found!
{
echo 'In test';
}
<script type="text/javascript"> //<--- Script tag found, dont do any matches!
$(function() {
function Test()
{
var bla = "In js";
}
});
</script> //<--- Closed tag, start searching for matches again.
public function Test($name) //<--- Match found!
{
echo 'In test';
}
How can i do this in my regex?
As mentioned in the comments:
If your php functions always have a visibility modifier like
publicyou could do:Otherwise, you could strip the script part first.
Something like: