hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP: Variable as part of a text string
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

PHP: Variable as part of a text string

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 03-24-2006, 11:14 AM
mylinear mylinear is offline
Junior 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?

Reply With Quote


Sponsored Links
  #2  
Old 03-24-2006, 11:33 AM
tiamak tiamak is offline
Junior Guru
 
Join Date: Oct 2004
Location: Złocieniec, Poland
Posts: 190
simply use str_replace/preg_replace function

__________________
www.goscinnawies.pl - family business, small travel agency in Poland


Reply With Quote
  #3  
Old 03-24-2006, 11:38 AM
tanfwc tanfwc is offline
Web Hosting Master
 
Join Date: Apr 2004
Location: Singapore
Posts: 1,459
Code:
<?

$color = 'blue';
$text = "The sky is ".$color.".";
print $text;
?>
Is this something you are looking at?

__________________
tanfwc

Reply With Quote
Sponsored Links
  #4  
Old 03-24-2006, 12:19 PM
mylinear mylinear is offline
Junior Guru Wannabe
 
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

Reply With Quote
  #5  
Old 03-24-2006, 12:20 PM
mylinear mylinear is offline
Junior Guru Wannabe
 
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.

Reply With Quote
  #6  
Old 03-24-2006, 12:29 PM
tiamak tiamak is offline
Junior Guru
 
Join Date: Oct 2004
Location: Złocieniec, Poland
Posts: 190
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


Reply With Quote
  #7  
Old 03-24-2006, 12:34 PM
mylinear mylinear is offline
Junior Guru Wannabe
 
Join Date: Jul 2005
Posts: 77
Thanks to tiamak and tanfwc for the help.

Reply With Quote
  #8  
Old 03-24-2006, 12:42 PM
tiamak tiamak is offline
Junior Guru
 
Join Date: Oct 2004
Location: Złocieniec, Poland
Posts: 190
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


Reply With Quote
  #9  
Old 03-25-2006, 01:44 AM
Burhan Burhan is offline
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,100
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.

__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Strategies to Reduce Operating Costs, Avoid Email Blacklisting and Improve Deliverability Webinars 2013-05-15 11:27:46
WHD.india 2013 Web Hosting Events 2013-04-24 18:18:58
Are you ready for Speed Networking at WHIR Events? Blog 2013-01-18 07:00:04
Irish Web Host LetsHost Sending Billing Reminders to Customers via Text Message Web Hosting News 2012-03-13 17:00:48
The Variable Requirements of Data Center Vendor Selection: Andrew Schaap of Digital Realty Trust Web Hosting News 2011-09-27 15:06:29


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?