Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2008, 09:31 PM   PM User | #1
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
Preg_match??

Hi

I have a document like

Code:
<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
__________________
flying dagger
xiaodao is offline   Reply With Quote
Old 12-07-2008, 10:43 PM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
PHP Code:
<?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
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
xiaodao (12-08-2008)
Old 12-08-2008, 02:25 AM   PM User | #3
xiaodao
Regular Coder

 
Join Date: Sep 2004
Posts: 713
Thanks: 6
Thanked 2 Times in 2 Posts
xiaodao is an unknown quantity at this point
thank you very much
__________________
flying dagger
xiaodao is offline   Reply With Quote
Old 12-08-2008, 02:35 AM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by xiaodao View Post
thank you very much
you are welcome,

best regards
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:15 PM.


Advertisement
Log in to turn off these ads.