Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Preg_match??

  1. #1
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts

    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

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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

  • Users who have thanked oesxyl for this post:

    xiaodao (12-08-2008)

  • #3
    Regular Coder
    Join Date
    Sep 2004
    Posts
    713
    Thanks
    6
    Thanked 2 Times in 2 Posts
    thank you very much
    flying dagger

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by xiaodao View Post
    thank you very much
    you are welcome,

    best regards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •