SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Thread: $luke -> {luke}

  1. #1
    Fully Qualified Fool :) luke-innovative's Avatar
    Join Date
    Jun 2002
    Location
    Kent, UK
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $luke -> {luke}

    Hey,

    Just wondering if anyone knew how I could replace a variable '$luke' with '{luke}' to output in html.

    Example:
    PHP Code:
    $luke "Luke Martin" 
    Code:
    <html>
    ...
    <body>
    My name is {luke}
    </body>
    ...
    </html>
    I asked this in a previous thread but I didn't get any replies and I think it may be useful to others aswell

    Thanks
    Luke
    Luke-Martin.com (work in progress) :: Independent Web Designer's Portal
    Freelance Forums :: Sign Up Here
    "What happens if you get scared half to death twice?"

  2. #2
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe there are a few ways to do it:
    PHP Code:
    $page "<html>
               <head>
                 <title></title>
               </head>
               <body>
                 My name is 
    $luke
               </body>
             </html>
             "
    ;
    echo 
    $page
    PHP Code:
    $page '<html>
               <head>
                 <title></title>
               </head>
               <body>
                 My name is '
    .$luke.'
               </body>
             </html>
             '
    ;
    echo 
    $page
    This would be the closest thing to what you are asking.
    PHP Code:
    <html>
      <head>
        <title></title>
      </head>
      <body>
        My name is <?php echo $luke?>
      </body>
    </html>
    John

  3. #3
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a whole lot easier (and more useful!) when the data is in an array.
    for example:
    PHP Code:
    $name[luke] = "Luke Martin";
    foreach (
    $name as $key=>$val)
    {
        echo(
    '{' $key '}');

    Does that help at all?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  4. #4
    Idea Developer
    Join Date
    Sep 2000
    Location
    Bethlehem, PA
    Posts
    521
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just use a php template engine like smarty
    Professional PHP programing / Hosting
    aim: downtoi3iz icq: 74637813

  5. #5
    Fully Qualified Fool :) luke-innovative's Avatar
    Join Date
    Jun 2002
    Location
    Kent, UK
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    John: I know I can output the variable like you said, but that's not really what I need

    Sam: I think it is, but could I output that on a HTML page like I said ?

    Necro: I'm doing this to learn something, there's not a real use for it, I'd prefer to learn how it's done. I'm not to keen on using a template engine.

    In SomoneWhoIs's thread, someone posted how to do what I ask from an array of results from a DB - but I want to be able to do it with just one value. I also wanted to find out how HTML can interpret {Luke} and put in the value.


    Cheers again
    Luke
    Luke-Martin.com (work in progress) :: Independent Web Designer's Portal
    Freelance Forums :: Sign Up Here
    "What happens if you get scared half to death twice?"

  6. #6
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by luke-innovative
    Sam: I think it is, but could I output that on a HTML page like I said ?
    Yes, if you can issolate it in a variable, you can print it to an html page.

    Let me see if I understand the initial question, I think everyone's a little confused... lets say you have:
    $bill = 'William Smith';

    Do you want to be able to print to the html page:
    "Hi, my name is bill"
    or
    "Hi, my name is William"
    ?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  7. #7
    Fully Qualified Fool :) luke-innovative's Avatar
    Join Date
    Jun 2002
    Location
    Kent, UK
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "Hi, my name is William Smith"
    Luke
    Luke-Martin.com (work in progress) :: Independent Web Designer's Portal
    Freelance Forums :: Sign Up Here
    "What happens if you get scared half to death twice?"

  8. #8
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by luke-innovative
    "Hi, my name is William Smith"
    Isn't that the same as outputting the variable?

  9. #9
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I see. You want to make a templating system like smarty, basically?

    One flexible way to replace text using rules is use regular expressions.
    $replacement = preg_replace_all('/\{(.*?)\}/', "$$1", $source);

    Haven't tested that! It's possible the } doesn't need a \ before it. Or i could have screwed something else up.
    Does that look like I might have your question correct?
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  10. #10
    Fully Qualified Fool :) luke-innovative's Avatar
    Join Date
    Jun 2002
    Location
    Kent, UK
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Possibly
    I've never really used any of the replaces before, that's why I'm unsure

    So in your code, what is '$source'? Is '$1' my '$luke' variable?

    Thanks
    Luke
    Luke-Martin.com (work in progress) :: Independent Web Designer's Portal
    Freelance Forums :: Sign Up Here
    "What happens if you get scared half to death twice?"

  11. #11
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heh, now I'm stuck

    I was thinking this would work, but it doesn't quite:
    Code:
    <?php
    $name = 'Luke';
    $source = "<html>
               <head>
                 <title></title>
               </head>
               <body>
                 My name is {name}
               </body>
             </html>
             ";
    $replacement = preg_replace('/\{(.*?)\}/', "$$1", $source);
    echo($replacement);
    ?>
    I'll figure out something that does work later... or perhaps someoen else will beat me to it.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  12. #12
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, seems you can't use variable variables in a preg replacement. Bummer.
    Here's my workaround, which is a little convoluted:
    Code:
    <?php
    // serious application stuff up here or in includes
    ?>
    $name = 'Luke';
    $source = "<html>
               <head>
                 <title></title>
               </head>
               <body>
                 My name is {name}
               </body>
             </html>
             ";
    preg_match('/\{(.*?)\}/', $source, $matches);
    $replacement_item = $$matches[1];
    $replacement = preg_replace("/\{$matches[1]\}/", "$replacement_item", $source);
    echo($replacement);
    ?>
    There was a good Voostind thread about templating a while back: http://www.sitepointforums.com/showt...threadid=67849 (check it out, scroll down to Voostind's long post) ... one of the points made was that creating "templating tags" (such as smarty does) is unneeded... with good application design, the parts of the page will be divided into functions.
    Example:
    PHP Code:
            <html>
               <head>
                 <title></title>
               </head>
               <body>
                 My name is <?=$identity->displayName();?>
               </body>
             </html>
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?

  13. #13
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aright, I might be missing something. Just what are the {} supposed to do in HTML?
    John

  14. #14
    Fully Qualified Fool :) luke-innovative's Avatar
    Join Date
    Jun 2002
    Location
    Kent, UK
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    {} enable PHP stuff to be outputted on HTML

    The whole idea was so that my pages would be neater and easier to edit, but I suppose there are easier ways

    Thanks a lot everyone - helped loads, but consider this thread closed now


    Thanks again
    Luke
    Luke-Martin.com (work in progress) :: Independent Web Designer's Portal
    Freelance Forums :: Sign Up Here
    "What happens if you get scared half to death twice?"

  15. #15
    if($awake){code();} PHP John's Avatar
    Join Date
    Jul 2002
    Location
    Along the Wasatch Fault line.
    Posts
    1,771
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was my understanding that the <?php ?> tags were used for that. Am I missing another way of doing this? If so, where can I read about it (as I have not seen this before).

    John

  16. #16
    ********* Wizard silver trophy Cam's Avatar
    Join Date
    Aug 2002
    Location
    Burpengary, Australia
    Posts
    4,495
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Try this - http://smarty.php.net

    It's a PHP templating engline which does exactly what you're talking about (I think).

  17. #17
    SitePoint Wizard samsm's Avatar
    Join Date
    Nov 2001
    Location
    Atlanta, GA, USA
    Posts
    5,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by PHP John
    It was my understanding that the <?php ?> tags were used for that.
    You've hit the nail on the head... that's pretty much the problem with smarty. PHP is already a templating system. Why create a whole new syntax for the same thing?

    Voostind lays this out articulately in the aforementioned highly recommended thread: http://www.sitepointforums.com/showt...threadid=67849

    For academic purposes, I bet smarty does work something like the code I posted, just more clever, probably. One could find that out by looking at Smarty's source. But if you were to adjust my code to do a foreach loop with $matches after the first preg_match, you could easily replace all the {markup} in a document.
    Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?


Bookmarks

Posting Permissions

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