Prismatic Posted October 28, 2008 Share Posted October 28, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/ Share on other sites More sharing options...
Prismatic Posted October 28, 2008 Author Share Posted October 28, 2008 Doh. Figured it out lol $code = preg_replace("/(.*?)\((.*?)\)/", "[b]\\1([/b]\\2[b])[/b]", $code); Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/#findComment-676893 Share on other sites More sharing options...
Prismatic Posted October 28, 2008 Author Share Posted October 28, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/#findComment-676894 Share on other sites More sharing options...
nrg_alpha Posted October 28, 2008 Share Posted October 28, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/#findComment-676920 Share on other sites More sharing options...
nrg_alpha Posted October 28, 2008 Share Posted October 28, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/#findComment-676932 Share on other sites More sharing options...
discomatt Posted October 28, 2008 Share Posted October 28, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/#findComment-677008 Share on other sites More sharing options...
Prismatic Posted October 29, 2008 Author Share Posted October 29, 2008 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 =/ Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/#findComment-677056 Share on other sites More sharing options...
ghostdog74 Posted October 29, 2008 Share Posted October 29, 2008 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"; } } Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/#findComment-677080 Share on other sites More sharing options...
nrg_alpha Posted October 29, 2008 Share Posted October 29, 2008 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; Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/#findComment-677102 Share on other sites More sharing options...
ghostdog74 Posted October 29, 2008 Share Posted October 29, 2008 You might want to read the entire thread Ghostdog.. that issue has already been solved.. i am providing another way to solve that issue without messy regexp. What's wrong with that? Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/#findComment-677147 Share on other sites More sharing options...
Prismatic Posted October 29, 2008 Author Share Posted October 29, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/130485-simple-pattern-match-help/#findComment-677157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.