Results 1 to 9 of 9
-
03-24-2006, 11:14 AM #1Junior Guru Wannabe
- 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?
-
03-24-2006, 11:33 AM #2Junior Guru
- 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
-
03-24-2006, 11:38 AM #3Web Hosting Master
- Join Date
- Apr 2004
- Location
- Singapore
- Posts
- 1,522
Code:<? $color = 'blue'; $text = "The sky is ".$color."."; print $text; ?>
tanfwc
-
03-24-2006, 12:19 PM #4Junior Guru Wannabe
- Join Date
- Jul 2005
- Posts
- 77
Originally Posted by tiamak
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
-
03-24-2006, 12:20 PM #5Junior Guru Wannabe
- Join Date
- Jul 2005
- Posts
- 77
Originally Posted by tanfwc
"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.
-
03-24-2006, 12:29 PM #6Junior Guru
- Join Date
- Oct 2004
- Location
- Złocieniec, Poland
- Posts
- 191
Originally Posted by mylinear
u need to use *replace functionwww.goscinnawies.pl - family business, small travel agency in Poland
-
03-24-2006, 12:34 PM #7Junior Guru Wannabe
- Join Date
- Jul 2005
- Posts
- 77
Thanks to tiamak and tanfwc for the help.
-
03-24-2006, 12:42 PM #8Junior Guru
- 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
-
03-25-2006, 01:44 AM #9Web Hosting Master
- 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;');