hosted by liquidweb


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

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
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?



Sponsored Links
  #2  
Old
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


  #3  
Old
Web Hosting Master
 
Join Date: Apr 2004
Location: Singapore
Posts: 1,494
Code:
<?

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

__________________
tanfwc
Singapore Managed Colocation
Singapore BGP Announcement

Sponsored Links
  #4  
Old
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

  #5  
Old
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.

  #6  
Old
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


  #7  
Old
Junior Guru Wannabe
 
Join Date: Jul 2005
Posts: 77
Thanks to tiamak and tanfwc for the help.

  #8  
Old
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


  #9  
Old
Community Guide
 
Join Date: Jul 2003
Location: Kuwait
Posts: 5,099
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

Related posts from TheWhir.com
Title Type Date Posted
Do Offsite Search Marketing Strategies Still Matter? Blog 2014-01-15 16:30:47
The Value of Partnerships Part 2 - Giving Customers What They Want Blog 2014-04-24 13:27:36
Strategies to Reduce Operating Costs, Avoid Email Blacklisting and Improve Deliverability Webinars 2014-06-10 11:05:29
The Value of Partnerships Part 1: Leveraging an Existing Customer Base Blog 2014-04-24 13:27:37
Are you ready for Speed Networking at WHIR Events? Blog 2013-01-18 07:00:04


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?
WHT Host Brief Email:

We respect your privacy. We will never sell, rent, or give away your address to any outside party, ever.

Advertisement:
Web Hosting News:
WHT Membership
WHT Membership



 

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?