PHP: Function call inside replace function

LincLinc OwnerDetroit Icrontian
edited March 2007 in Internet & Media
I need someone to explain how the hell this piece of code works (from vBulletin):

[PHP]$bbcode = preg_replace('#\[img\]\s*(https?://([^<>*"]+|[a-z0-9/\\._\- !]+))\[/img\]#iUe', "\$this->handle_bbcode_url(str_replace('\\\"', '\"', '\\1'), '')", $bbcode);
[/PHP]
Basically, it's finding a link pattern and replacing it with the RESULT of passing the match to the function handle_bbcode_url. However... that doesn't work. How can a function name be in the middle of a string and execute? I can't figure this POS out to save my life. :(

Comments

  • RWBRWB Icrontian
    edited March 2007
    Not to crap your thread, but I forgot what the hell that's called... the pattern finding portion that looks like random characters that is.... I think it started with an R.
  • primesuspectprimesuspect Beepin n' Boopin Detroit, MI Icrontian
    edited March 2007
    regular expressions
  • ShortyShorty Manchester, UK Icrontian
    edited March 2007
    How can a function name be in the middle of a string and execute

    Functions can be layered into functions.

    Eg.. str_replace(stripslashes(url_encode($some_value)));

    It will always do the inner most function first. In this instance.. url_encode :)
  • LincLinc Owner Detroit Icrontian
    edited March 2007
    I know you can layer functions, but look what's specifically happening here - the call to "handle_bbcode_url" is INSIDE the STRING of the replace string for preg_replace. That will cause the NAME of the function to return, along with it's parameters (not its output like I want).

    According to that example, it should resolve itself somehow somewhere, but i can figure out why or where that it is.
  • ShortyShorty Manchester, UK Icrontian
    edited March 2007
    [PHP]$bbcode = preg_replace('#\[img\]\s*(https?://([^<>*"]+|[a-z0-9/\\._\- !]+))\[/img\]#iUe', $this->handle_bbcode_url(str_replace('\\\"', '\"', '\\1'), ''), $bbcode); [/PHP]

    ???
  • LincLinc Owner Detroit Icrontian
    edited March 2007
    The variables aren't passed then. \\1 is supposed to be the first match. If you do it like that, it literally is passed as "\\1" instead of becoming the variable. :-/
  • LincLinc Owner Detroit Icrontian
    edited March 2007
    Ahhh, I found my problem (after much head-banging). I was using 'eregi_replace' instead of 'preg_replace' in my version, AND you need an 'e' modifier on the end of the pattern.

    In other news, I've pretty much decided eregi_replace is all-around worthless and does nothing but screw me up when I've used it.
Sign In or Register to comment.