Activity Stream
48,167 MEMBERS
61110 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Results 1 to 3 of 3
  1.     
    #1
    Member
    Website's:
    CoursesWeb.net MarPlo.net

    Default 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?
    MarPlo Reviewed by MarPlo on . Replace with Dynamic variable in preg_replace Hi I'm trying the following code: $t = '12<-- AB_C -->'; $AB_C = 'abc'; echo preg_replace('/\<-- (+) --\>/', "$$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? Rating: 5

  2.   Sponsored Links

  3.     
    #2
    Respected Member
    I am not really sure what you are trying to do here.

    If you are trying to get the contents of the variable $AB_C to replace the string "<-- AB_C -->" in the example or what?

    But if that is the case just use this:
    PHP Code: 
    <?php
    $t 
    '12<-- AB_C -->';
    $AB_C 'abc';
    echo 
    preg_replace('/\<-- ([A-Z_]+) --\>/', &$AB_C$t);
    ?>

  4.     
    #3
    Member
    Website's:
    CoursesWeb.net MarPlo.net
    Hi,
    I solved the problem using the "/e" flag, which makes the preg_replace() to evaluate the expresion before replacement.
    Code: 
    preg_replace('/\<-- ([A-Z_]+) --\>/e', "$$1", $t);

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Preg_replace help
    By Gavo in forum Web Development Area
    Replies: 3
    Last Post: 25th Sep 2012, 07:23 PM
  2. Wordpress title preg_replace
    By energyboost in forum Wordpress
    Replies: 2
    Last Post: 3rd Oct 2011, 11:47 PM
  3. Lulzimg preg_replace help
    By masterb56 in forum Web Development Area
    Replies: 2
    Last Post: 15th Aug 2011, 06:31 PM
  4. preg_replace first half of matches ?
    By Porsche_maniak in forum Web Development Area
    Replies: 1
    Last Post: 16th Jan 2011, 05:12 PM
  5. Variable in phpBB
    By SarMa in forum phpBB
    Replies: 11
    Last Post: 26th Mar 2010, 07:08 PM

Tags for this Thread

BE SOCIAL