[SOLVED] preg_replace \\1 inside function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

[SOLVED] preg_replace \\1 inside function

Post by phice »

I'm trying to get the value of \\1 into a function.

My code:

Code: Select all

$this->body = preg_replace("#\[attachment=(.*?)\]#si",           attachment("\\1"), $this->body);
I need to get the value of \\1 into the attachment() function. I'm sure it's possible, any help?
Last edited by phice on Thu Apr 15, 2004 7:30 am, edited 1 time in total.
Image Image
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Thanks! :)

For future reference (visitors looking for a default answer), here's the code that I used:

Code: Select all

$this->body = preg_replace_callback("#\[attachment=(.*?)\]#si",  create_function('$matches', 'return attachment($matches[1]);'), $this->body);
Image Image
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

omg phice i love you

ive had this problem for weeks!!!!

full steam ahead!!!
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

whats so great about this \\1 anyways?
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

\\1 returns the value of the first (hint: 1) (.*?) in the first set of quotes.
Image Image
Post Reply