Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2005
    Posts
    77

    PHP: Variable as part of a text string

    The data in a text field of a MySQL table is literally as follows:
    The sky is $color

    The data is read into a variable called $text.

    <?php
    print $text;
    ?>
    will output: The sky is $color

    Is it possible to somehow treat the $color as a variable and parse it to output a value? For example:
    <?php
    $color = "blue";
    prit $text; // can something be done here?
    ?>
    to get the output: The sky is blue

    Or can the data in the text field be stored in some other way to get the desired output? Maybe with additional quotes around $color or something?

  2. #2
    Join Date
    Oct 2004
    Location
    Złocieniec, Poland
    Posts
    191
    simply use str_replace/preg_replace function
    www.goscinnawies.pl - family business, small travel agency in Poland

  3. #3
    Join Date
    Apr 2004
    Location
    Singapore
    Posts
    1,522
    Code:
    <?
    
    $color = 'blue';
    $text = "The sky is ".$color.".";
    print $text;
    ?>
    Is this something you are looking at?
    tanfwc

  4. #4
    Join Date
    Jul 2005
    Posts
    77
    Quote Originally Posted by tiamak
    simply use str_replace/preg_replace function
    So I have to find the $color in the text field and replace it with "blue" and only then print out the text?

    Isn't there a way for PHP to do it automatically? For example:
    <?php
    $color = "blue";
    $text = "The sky is $color";
    print $text;
    ?>
    will print out: The sky is blue

    I was trying to do something like that, except in this case the $text content is read from a text field from a MySQL table which has the data The sky is $color

  5. #5
    Join Date
    Jul 2005
    Posts
    77
    Quote Originally Posted by tanfwc
    [code]<?
    Is this something you are looking at?
    Are you suggesting that I put
    "The sky is ".$color."."
    as the data in the table text field itself? I tried that but it did not work. It will print out exactly as the above, without converting $color to blue.

  6. #6
    Join Date
    Oct 2004
    Location
    Złocieniec, Poland
    Posts
    191
    Quote Originally Posted by mylinear
    Isn't there a way for PHP to do it automatically? For example:
    <?php
    $color = "blue";
    $text = "The sky is $color";
    print $text;
    ?>
    will print out: The sky is blue

    I was trying to do something like that, except in this case the $text content is read from a text field from a MySQL table which has the data The sky is $color
    well and thats the problem it wont work when u output string from db.
    u need to use *replace function
    www.goscinnawies.pl - family business, small travel agency in Poland

  7. #7
    Join Date
    Jul 2005
    Posts
    77
    Thanks to tiamak and tanfwc for the help.

  8. #8
    Join Date
    Oct 2004
    Location
    Złocieniec, Poland
    Posts
    191
    u may also do replacement in sql query

    PHP Code:
    $color='blue';
    mysql_query('select replace(field_name, "$color", "'.$color.'") FROM table WHERE ....'); 
    www.goscinnawies.pl - family business, small travel agency in Poland

  9. #9
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,104
    Or you can do this :

    PHP Code:
    $color 'red';
    $string "The sky is $color";
    eval(
    'echo $string;'); 
    But be warned, eval can lead to security problems in your code.

Posting Permissions

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