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
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?

 

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 =/

Link to comment
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?

 

 

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";
}
}

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.