Results 1 to 6 of 6

Thread: Replace text with variables in php

  1. #1
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Replace text with variables in php

    Hello, Im new to the forum.

    I want to load text from a .txt file. Then replace the preset variables with the ones I put into a form.

    I have a pre made template made already. EXAMPLE:

    "Hi, my name is $name , I like to play $sport, and I live in $city"

    I want to load that info from text, then change the variables to the actual words that was put in the form. (my text is way long that the text above, thats just a sample)

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    use sprintf():
    text.txt:
    Code:
    Hi, my name is %s, I like to play %s, and I live in %s
    PHP Code:
    <?php
    $text 
    file_get_contents'text.txt' );
    $name 'Bob';
    $sport 'baseball';
    $city 'Baltimore';

    print 
    sprintf$text,$name,$sport,$city );

  3. #3
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks for replying

    Im getting this

    Warning: sprintf() [function.sprintf]: Too few arguments in ..... on line 50


    This is my line 50

    $sample is my .txt


    print sprintf($sample, $linkz, $link2, $artist, $twitter, $song, $pic);




    The text doc. is actually an HTML code. and the Form will replace when I have $linkz $artist etc.
    Last edited by djnicemobile; 02-12-2012 at 04:59 AM.

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    what is the exact text you're using (i.e., the contents of $sample)?

    the number of tokens ( %s ) in your text needs to match the number of additional arguments you pass to sprintf().
    In your example (sprintf($sample, $linkz, $link2, $artist, $twitter, $song, $pic);), your text would need to contain six tokens.

  5. #5
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    It is quite long, its an email template. (long html code)...


    or is there a way I can place the html code in the actual php file..

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djnicemobile View Post
    It is quite long, its an email template. (long html code)...


    or is there a way I can place the html code in the actual php file..
    no, it's better to keep it separate.

    are you trying to use those same variables in several places in the template? In that case, you can number them... here's an example:
    PHP Code:
    <?php
    $text 
    'My name is %1$s, and I like %2$s. Remember my name - %1$s.';
    $name 'Joe Cool';
    $favorite 'PHP';

    printf$text,$name,$favorite );
    // prints "My name is Joe Cool, and I like PHP. Remember my name - Joe Cool."
    you'll still need to pass as many extra arguments as there are unique tokens in the template.

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
  •