SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    Replace with Dynamic variable in preg_replace

    Hi
    I'm trying the following code:
    Code:
    $t = '12<-- AB_C -->';
    $AB_C = 'abc';
    echo preg_replace('/\<-- ([A-Z_]+) --\>/', "$$1", $t);
    I want to get "12abc" , but it outputs: 12$AB_C , so, it not recognize the replacement as dynamic variable.
    Is it any way to use the matched word in preg_replace() as a variable, or dynamic variable?
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    For those who look for a solution to this problem, the '/e' flag, which evalates the replacement, solved the problem, and returns the results i want, using:
    Code:
    preg_replace('/\<-- ([A-Z_]+) --\>/e', "$$1", $t);
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •