Forum Moderators: coopster

Message Too Old, No Replies

preg_replace backreferences to functions?

Boy would it make my life easier if you could pass backreferences to funcs!

         

Fuzzplug Jones

3:03 am on Oct 6, 2005 (gmt 0)

10+ Year Member



Hello, first post!

I'm trying to build a wiki syntax parser with preg_replace and arrays for the search and replace items. I've made some strides, but I'm having problems with one thing.

At one point I need to convert link names to underscored_link_names. I know a little regex and I know how to use backreferences in the replacement strings. However, in this case I need to send the backreference to a function (or at least to str_replace(), but I made myself a function called "addunderscores()" to keep the code clean).

However, I can't seem to succesfully pass a backreference to a function from preg_replace.

Is there a way to do this? I hope so, because it seems like doing it any other way is going to be 10 times as difficult and more manual.

Thanks.

mincklerstraat

10:44 am on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to look at example #5 in the page in the php manual on preg_replace, which shows how to use the "e" modifier, which will allow you to use code in your replace string with your backreferences - in that code you can use your own function or str_replace. It's a bit gnarly with all the escaping you have to do, but follow the example as closely as you can and you'll succeed.

coopster

1:35 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




It's a bit gnarly with all the escaping you have to do

True, regex's can get that way sometimes when they are a bit advanced. Don't forget there is always the preg_quote() [php.net] function that can sometimes alleviate the "gnarlyness".

And welcome to WebmasterWorld, Fuzzplug Jones.

ergophobe

3:53 pm on Oct 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



And single-quoted strings so you don't have to count how many times your $x will get evaluated.

Fuzzplug Jones

4:18 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



This looks like it'll work. I'll give it a try later when I have some time to sit down with the code. I'm kind of embarassed it was right there in front of me, but I appreciate the flame-less nudge to the right example.

mincklerstraat

8:49 am on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't be embarassed. I also tend to sorta "skim" the php manual and I don't know how many times I skimmed the preg_replace page until I came upon option e - in fact, I think I came across it in on the modifiers page, and thought, "hey wow, it can do that!". Was more than a year after I'd started working with preg_replace(). Nb: careful what you put in there if you'll ever have to modify your code, it can result in really ugly, ugly code you never ever want to go back to. If it's not repeated a whole lot, break up the stuff in there into functions so you can read it better.

Fuzzplug Jones

8:09 pm on Oct 7, 2005 (gmt 0)

10+ Year Member



Thanks. I'm not necessarily embarrased for skimming the docs, what I meant was most PHP help places (even #php on Freenode), they're kind of rude when the answer turns up to be right in front of you. So WebmasterWorld is even cooler now, because you guys didn't do that :-).