Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Run a function from inside a string ??? 1

Status
Not open for further replies.

Sleidia

Technical User
Joined
May 4, 2001
Messages
1,284
Location
FR
Hello,

Here is an interesting problem I'm facing.
I've created a function called make_query() whose purpose is to allow me to use one single syntax for INSERT, UPDATE and SELECT commands.

There are several options and one of them (the last one) makes it possible to use the variable $template so that all the records are diplayed with the proper layout. As you can see, the function replaces all the words enclosed in "][" with the proper values taken from the database.

$template = "

<b>]news_date[</b> ]news_title[<br>
]news_desc[<br>
<br>

";

make_query("select", "my_table", "news_title . news_desc . news_date", "ORDER BY news_date ASC", NULL, $template);

print($output);

The problem is that now, I'd like to apply a function to the variable $news_date for example.

$template being a string, I'm wondering how I could have the name of the function written within this string so that $news_date would be altered by the function after all the database values are found in the string.

The first thing I've tried was :

$template = "

<b>" . format_sqldate("]news_date[", 4) . "</b> ]news_title[<br>
]news_desc[<br>
<br>

";

... which could never work of course.

Any idea?

Thanks a lot to the ones who will respond ! :)
 
I assume that your question is not about replacing the template place-holders with the variables' values.

If you want to indicate within the template string to apply functions to values before they are placed into the template string, you're probably going to have to write a lexer to analyze the string and invoke the needed function(s).



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks Sleipnir ...

... but instead, I think I should find a method that would allow me to have a function written inside the string in a way that would prevent it from running.

something like : $string = "some text a_function(]string_to_be_replaced_by_var[) some text";

Then, my own function discussed above would put values in in the string:

Thus becoming something like : $string = "some text a_function($my_value) some text";

Lastly, some sort of eval() function would run a_function() so that $my_value is modified.

I've tried with eval() and read but still, I find no way to achieve it.
 
I'm still searching and here is what I've managed to do so far :

<?php

$template = "

<b> } format_sqldate(\" } news_date { \", 4) { </b> : } news_title { <br>

";

$output = make_query("select", "my_table", "news_title . news_date", "ORDER BY news_date ASC", NULL, $template);

$output = str_replace(" { ", " . \"", str_replace(" } ", "\" . ", $output));

eval (' ?>' . $output . '<?php ');

?>

Which would output the following in the browser :

" . format_sqldate("2004-06-05", 4) . " : news 1
" . format_sqldate("2004-06-06", 4) . " : news 2
" . format_sqldate("2004-06-07", 4) . " : news 3

As one can see, the function format_sqldate() isn't parsed by PHP.
That really bugs me !



 
You established in your first post that PHP isn't going to parse the string for you.

Again, if you want to do it that way, you're going to have to write a lexical analyzer yourself.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Well, the more I read here and there the more I think that eval() could do the trick.

When I simply use : eval($output);
I get: Parse error: parse error, unexpected '<' in [ ... path & file ... ] : eval()'d code on line 3

Which means that eval is doing his duty but some character in the string causes it to fail. Debugging it is quite difficult ;(
 
Yes, the HTML.

eval() is going to attempt to execute the entire string, not just the function call references. You might be able to play with PHP tags ("<?php", "?>") to get it to work, but I don't know for sure.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I DID IT !!!!!!!!
woooooooooo !!!!! :):):):):):):):):)

IT WORKS NOW .... pfe ;(

The solution :

<?php

$template = "
print('<b> } format_sqldate(' } news_date { ', 4) { </b> } news_title { <br>
} news_desc { <br>
<hr size=\"1\" width=\"100%\" color=\"#000000\">
<br>');
";

make_query("select", "my_table", "news_title . news_desc . news_date", "ORDER BY news_date ASC", NULL, $template);

$output = str_replace(" { ", " . '", str_replace(" } ", "' . ", $output));

eval($output);

?>

I had to :

1) include print() inside $template.
2) Replace \" by ' inside the functions found in $template.

Now, I can apply any function in the template :)

I just have to move the str_replace function inside make_query() and it will be perfect.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top