Current location: Hot Scripts Forums » Programming Languages » PHP » regex, need to put found value in replacement brackets


regex, need to put found value in replacement brackets

Reply
  #1 (permalink)  
Old 02-14-04, 06:17 PM
windwind windwind is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
regex, need to put found value in replacement brackets

I need to dynamically replace certain values using regular expressions.

It's for a template engine that looks for <: and :> brackets with text between them.

The problem is it'll reconize any of the brackets (<:title:>, <:main:> etc) but I need it to dynamically insert the replcaement with the correct value from an array.

Here is the array

PHP Code:

$TemplateTags = array(

            
'title' => "$Title",
            
'menu' => "menu",
    ); 
It has the title and menu.

Here is the regex

PHP Code:

$ParseTitle preg_replace("/<:.*?:>/is""$TemplateTags[title]"$Page); 

That works, but, it'll replace every set of tags with the title value, I need to take the value the regex finds, and place that value in the replacement array.

This was suggested to me at devshed, but it causes an error.

PHP Code:

$ParseTitle preg_replace("/<:.*?:>/is""$TemplateTags[$$1]"$Page); 

Anyone know how to carry over the values?
Reply With Quote
  #2 (permalink)  
Old 02-14-04, 08:06 PM
nd2 nd2 is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 128
Thanks: 0
Thanked 0 Times in 0 Posts
$ParseTitle = preg_replace("/<.*?):>/is", $TemplateTags["$1"], $Page);

try that.
__________________
IonCMS (Coming Soon.)
http://ioncms.com
--
Ncaster (Free php/mysql cms)
http://ncaster.cjb.net
Reply With Quote
  #3 (permalink)  
Old 02-15-04, 01:22 AM
windwind windwind is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Produces an error also, unfortunately.
Reply With Quote
  #4 (permalink)  
Old 02-15-04, 04:04 AM
0zone 0zone is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
This will do the trick, note that the template variables can't have '>' or ':' in them.

Code:
<?php

	$tags = array(
		'title' => 'something',
		'menu' => 'this is the menu',
	);
	
	$template = '<:title:> and \'<:menu:>\'';
	
	echo preg_replace('/<:([^:>]*):>/ie', '$tags[\'\1\']', $template); 

?>
Reply With Quote
  #5 (permalink)  
Old 02-15-04, 02:22 PM
windwind windwind is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
It works perfectly,

now all I need to do is include the template and I'm gold.

Thanks.
Reply With Quote
  #6 (permalink)  
Old 02-15-04, 08:45 PM
0zone 0zone is offline
Newbie Coder
 
Join Date: Feb 2004
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
No problem
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 08:51 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.