Results 1 to 3 of 3
-
20th Feb 2012, 06:39 AM #1OPMemberWebsite's:
CoursesWeb.net MarPlo.netReplace 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);
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: 5Free Courses HTML, CSS, Ajax, Flash, ActionScript for Web Masters.
Free PHP - MySQL Course, JavaScript - jQuery Course and tutorials.
-
20th Feb 2012, 05:58 PM #2Respected 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);
?>
-
21st Feb 2012, 08:36 AM #3OPMemberWebsite's:
CoursesWeb.net MarPlo.netHi,
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);
Free Courses HTML, CSS, Ajax, Flash, ActionScript for Web Masters.
Free PHP - MySQL Course, JavaScript - jQuery Course and tutorials.
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
Preg_replace help
By Gavo in forum Web Development AreaReplies: 3Last Post: 25th Sep 2012, 07:23 PM -
Wordpress title preg_replace
By energyboost in forum WordpressReplies: 2Last Post: 3rd Oct 2011, 11:47 PM -
Lulzimg preg_replace help
By masterb56 in forum Web Development AreaReplies: 2Last Post: 15th Aug 2011, 06:31 PM -
preg_replace first half of matches ?
By Porsche_maniak in forum Web Development AreaReplies: 1Last Post: 16th Jan 2011, 05:12 PM -
Variable in phpBB
By SarMa in forum phpBBReplies: 11Last Post: 26th Mar 2010, 07:08 PM
themaCreator - create posts from...
Version 3.24 released. Open older version (or...