Jump to content

Simple pattern match help


Prismatic

Recommended Posts

I'm attempting to match any string that looks like this

bleh(stuff)

 

bleh and stuff can be anything..

 

this is the code I have, but it's obviously wrong since it doesn't work

 

$code = preg_replace("/(.*?)((.*?))", "[b]\\1([/b]\\2[b])[/b]", $code);  

 

I'm building a code formatter and trying to match functions so I can make them bold.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/
Share on other sites

Unsolved this because I discovered an issue

 

It's matching the entire string, for example

 

function LoadGif($imgname)  

 

Only LoadGif($imgname) should be found and made bold, however it's also pulling in everything to the left of that too

 

Any ideas on how to make it not go back like that?

 

Is this what you are looking for?

 

$str = 'function LoadGif($imgname)';
$newStr = preg_replace('#( [^(]+\()([^)]+)\)#', " [b]$1[/b]$2[b])[/b]", $str);
echo $newStr;

 

EDIT: This would work with the above example, but not something like: $t = function LoadGif($imgname)

I'll be back shortly with the solution.

 

EDIT 2:

This should do it, no?

$newStr = preg_replace('#([^ (]+\()([^)]+)\)#', " [b]$1[/b]$2[b])[/b]", $str);

Well, if you want to follow PHP's function naming spec.

 

'/[a-z_\x7f-\xff][a-z0-9_\x7f-\xff]++\([^)]++\)/i'

 

This is assuming you won't use literal strings with closing brackets in your function... for example

 

bleh('Stuff with a ) closing bracket')

 

If you want something like that, the expression becomes quite a bit more complex :)

Oops.. forgot to eliminate that space in the replace double quotes..

 

$newStr = preg_replace('#([^ (]+\()([^)]+)\)#', "[b]$1[/b]$2[b])[/b]", $str);

 

Damn my head isn't clear today lol

 

How would I modify that to match nested functions as well?

 

if(is_numeric(666)) for example?

 

 

discomatt, your pattern didn't seem to match anything =/

Unsolved this because I discovered an issue

 

It's matching the entire string, for example

 

function LoadGif($imgname)  

 

Only LoadGif($imgname) should be found and made bold, however it's also pulling in everything to the left of that too

 

Any ideas on how to make it not go back like that?

 

 

split the string on space, go through each word and find "(" and ")". 

$string = 'function LoadGif($imgname)';
$s = explode(" ",$string);
foreach ($s as $k=>$v){
if ( strpos($v,"(") !== FALSE && strpos($v,")") !==FALSE){
    echo "Found : $v\n";
}
}

You might want to read the entire thread Ghostdog.. that issue has already been solved.. the OP now wants a nested function highlighted:

 

$str = 'if(is_numeric(666))';
$newStr = preg_replace('#(\w+\()    
                         ([^()]+)\)    
                         (.*)$#x', '[b]$1[/b]$2[b])[/b]$3', $str);
echo $newStr;

You might want to read the entire thread Ghostdog.. that issue has already been solved.. the OP now wants a nested function highlighted:

 

$str = 'if(is_numeric(666))';
$newStr = preg_replace('#(\w+\()    
                         ([^()]+)\)    
                         (.*)$#x', '[b]$1[/b]$2[b])[/b]$3', $str);
echo $newStr;

 

Works great, thank you!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.