473,324 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Resolving {$var} from within a string...

If i use

<?
$x = 4 ;
echo "This is the value of x: {$x}" ;
?>

i will obviously get the output
This is the value of x: 4

But when i have a string, in an SQL varchar field, such as
kra.php?EID={$eid}

I can then do $href = db_query($qry,0,"href") to get the SQL data into
$href, but when i echo $href, the $eid comes out as plain-text and
doesn't resolve to the current value of $eid.

Is it possible to force PHP to attempt to resolve any embedded $vars
within a string?
i.e. so when i output $href, i get
kra.php?EID=1

Thanks

Apr 16 '07 #1
4 1164
Chris Jones wrote:
Is it possible to force PHP to attempt to resolve any embedded $vars
within a string?
No.

But look at sprintf().

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Apr 16 '07 #2
"Toby A Inkster" <us**********@tobyinkster.co.ukwrote in message
news:p7************@ophelia.g5n.co.uk...
Chris Jones wrote:
>Is it possible to force PHP to attempt to resolve any embedded $vars
within a string?

No.
Wrong. It is possible with eval(), but it's strongly advaised not to use
this approach:
http://fi2.php.net/manual/en/function.eval.php

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
sp**@outolempi.net | rot13(xv***@bhgbyrzcv.arg)
Apr 16 '07 #3
On 16 Apr, 13:24, "Kimmo Laine" <s...@outolempi.netwrote:
"Toby A Inkster" <usenet200...@tobyinkster.co.ukwrote in messagenews:p7************@ophelia.g5n.co.uk...
Chris Jones wrote:
Cracked it.... the easiest way surely is just

$eid = $_GET['EID'];
$href = str_replace("{$eid}","{$eid}",db_result($qry,0,"hr ef");

:-)
>
Is it possible to force PHP to attempt to resolve any embedded $vars
within a string?
No.

Wrong. It is possible with eval(), but it's strongly advaised not to use
this approach:http://fi2.php.net/manual/en/function.eval.php

--
"Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpkhttp://outolempi.net/ahdistus/- Satunnaisesti päivittyvä nettisarjis
s...@outolempi.net | rot13(x...@bhgbyrzcv.arg)

Apr 18 '07 #4
On Apr 18, 2:57 pm, Chris Jones <chris...@gmail.comwrote:
$eid = $_GET['EID'];
$href = str_replace("{$eid}","{$eid}",db_result($qry,0,"hr ef");
Beware of case-sensitive behaviour of str_replace. Perhaps you should
replace str_replace() with str_ireplace() or just change the variable
name to uppercase. And dollar sign inside double quotes replaced with
its variable value.

$href = str_ireplace("{$eid}", $eid, db_result($qry,0,"href");
or
$href = str_replace("{\$EID}", $eid, db_result($qry,0,"href");

For a better approach, consider using preg_replace() with pattern like
"/\{\\\$EID\}/". Because preg_replace() can accept array as parameter,
so you can replace multiple variables in single step.
Another method is using preg_replace_callback() with reusable callback
function.

HTH

Apr 18 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and...
2
by: Glen | last post by:
Hi, Is there a way to resolve the current namespace in a static method, such as Main() from within a console app? In a public method from a class instance, you can use the "this" keyword in...
1
by: Stephen | last post by:
Hi all, is there a way that I can create an application that can "Resolve" any given address in the URL to be redirected to a default website. I guess that It can be done by "Resolving DNS...
1
by: Ghislain Tanguay | last post by:
Hi everyone, this is the code I use now for resolving domain address with email. But I use an external program "nslookup" to verify my addresses. Is there an intrinsic object in dotnet who do...
2
by: Frank | last post by:
Can I do this? I add a session var in C# and ultimatly want to pass it into a vbscript client side activeX control. This is what I have so far but get " Object Required:'name2' " error. Can...
5
by: Andrew Poulos | last post by:
If I have some code in a function that looks something like this: if (x) { for (var i = 0; i < a; i++) { //blah } } else { for (var i = 0; i < b; i++) { //blah }
7
by: Trac Bannon | last post by:
When I load XML from a file into a dotNet XMLDataDocument, the UTF-8 codes are resolved but the 5 special XML entities are not. How can I force those 5 special character types to be translated?
15
by: Jon Davis | last post by:
Bill Wagner posted something here .. http://msdn2.microsoft.com/en-us/vcsharp/default.aspx "Local Type Inference, Anonymous Types, and var" "Of all the features in C# 3.0, local type inference...
4
by: rhivka | last post by:
I'm going to try to keep this question within the student posting guidelines. I've been working on a class file, and I'm not sure I've constructed it correctly. I'm getting several errors concerning...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.