SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Oct 2004
    Location
    CA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool REALLY Simple and easy question about strings

    Ok so I'm retreving a row from a MySQL database, and text in that row contains variables. However when I echo it will show $variablename instead of parse the variables. Is there a function for that or something?

    Here is some code to explain better what I mean

    PHP Code:
    $text = 
    <?


    $name 
    'Eric';


    $text mysql_fetch_array(mysql_query("SELECT * FROM table LIMIT 1 ")); 
    // $text[field] is "Hi my name is $name"


    echo $text[field];
    // Outputs Hi my name is $name, not Hi my name is Eric


    ?>
    How do I make it output Hi my name is Eric??

  2. #2
    SitePoint Evangelist nsj's Avatar
    Join Date
    Oct 2005
    Location
    Jamaica (W.I)
    Posts
    447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, this might be a really stupid question but does "Eric" exist in your database and is it the first record?

    Second, if you have a field called "name" in your mysql table,
    try
    echo $text['name'];

    In this case, i dont see any use for $name = "Eric" as it's not used anywhere.

  3. #3
    SitePoint Member
    Join Date
    Oct 2004
    Location
    CA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No I mean that the record that is pulled from the database contains text with the variable $name in it.

    haha i suck at explaining stuff, in the above code the there is a column in the row that is pulled from the database called 'field', which is "Hi my name is $name", I would like $name in that string to change to "Eric"

  4. #4
    SitePoint Evangelist Rodney H.'s Avatar
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think a simple string replacement will work. Here is a simple example (sans db):


    PHP Code:
    <?php
    $name 
    'Eric';

    $text['field'] = 'Hi my name is $name';

    // Hi my name is $name
    echo $text['field'];

    // Hi my name is Eric
    echo str_replace('$name'$name$text['field']);
    ?>

  5. #5
    SitePoint Zealot Seriph's Avatar
    Join Date
    Oct 2005
    Location
    San Diego, CA
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could also use eval :

    PHP Code:
    <?php
    $name 
    'Eric';

    $text['field'] = 'Hi my name is $name';

    eval(
    "\$out_string = \"".$text['field']."\";");

    echo 
    $out_string;
    ?>

  6. #6
    SitePoint Member
    Join Date
    Oct 2004
    Location
    CA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    THX GUYS. eval() was exactly what I was looking for, I originally thought of str_replace but I thought that would take too long to get all the variables replaced.

    THANKS YOU VERY MUCH


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
  •