Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 356751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:08:28+00:00 2026-05-12T12:08:28+00:00

Do you know of any way to reference an object in the replacement part

  • 0

Do you know of any way to reference an object in the replacement part of preg_replace. I’m trying to replace placeholders (delimited with precentage signs) in a string with the values of attributes of an object. This will be executed in the object itself, so I tried all kinds of ways to refer to $this with the /e modifier. Something like this:

/* for instance, I'm trying to replace
 * %firstName% with $this->firstName
 * %lastName% with $this->lastName
 * etc..
 */
$result = preg_replace( '~(%(.*?)%)~e', "${'this}->{'\\2'}", $template );

I can’t get any variation on this theme to work. One of the messages I’ve been getting is: Can’t convert object Model_User to string.

But of course, it’s not my intention to convert the object represented by $this to a string… I want to grab the attribute of the object that matches the placeholder (without the percentage signs of course).

I think I’m on the right track with the /e modifier. But not entirely sure about this either. Maybe this can be achieved much more simple?

Any ideas about this? Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:08:28+00:00Added an answer on May 12, 2026 at 12:08 pm

    Like I commented to Paul’s answer: in the meanwhile I found the solution myself. The solution is much more simple than I thought. I shouldn’t have used double quotes.

    The solution is as simple as this:

    $result = preg_replace( '~(%(.*?)%)~e', '$this->\\2', $template );
    

    Hope this helps anyone else for future reference.

    Cheers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you know any elegant way to achieve this in ruby plz ? string
I'm wondering if there's any reasonable way to get a reference to an object
Is there anyway to pass the property of an Object by reference? I know
Does anyone know any simple way to retrieve the country from a given IP
Does anyone know of any way to convert a simple gif to xaml? E.G.
Do you know any easy way to find a logging call that throws not
just curious if you know of any way to setup a drag boundary for
By default nunit tests run alphabetically. Does anyone know of any way to set
Does anyone know of any 'standard' way to interface with a telephony system (think
I wish to know is there any way that I can create the threads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.