...

View Full Version : Preg_match??



xiaodao
12-07-2008, 10:31 PM
Hi

I have a document like



<p>hi {username}</p>
{content}


and now i have two variables like $name="haha", $content="kaak", how can i load the above document and replace {username} and {content} with the variables i define?

use preg_match? how to write the codes? if you can help, i will be very appreciate


Thanks
Xiaodao

oesxyl
12-07-2008, 11:43 PM
<?php
$tpl = "<p>hi {username}</p>
{content}
<p>hi {content}</p>
{username}";

$name = "haha";
$content = "kaak";

// take care to keys to be uniq!!!
$data = array('username' => $name,
'content' => $content);

function toreg($a){
return '/\{'.$a.'\}/';
}

$what = array_map("toreg",array_keys($data));
$with = array_values($data);
$res = preg_replace($what,$with,$tpl);

print $res;

?>


regards

xiaodao
12-08-2008, 03:25 AM
thank you very much

oesxyl
12-08-2008, 03:35 AM
thank you very much
you are welcome, :)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum