stuntpeople Posted October 30, 2007 Share Posted October 30, 2007 I have a file called 'news.php' from which I want to pull lines of text, inside which are some variables that are pre-assigned. As an example below, $movie_project_01 = "Titanic" and $current_location = "San Francisco" news.php 10/29/07: I've just finished $movie_project_01 and it's been great.<BR><BR> 10/24/07: I've changed our location to $current_location.<BR><BR> etc etc etc etc In my 'index.php' page, I have the following function that pulls $length lines from a file $filename and echoes each one index.php function getitems($filename, $length) { if (is_file($filename)) { $lines = file($filename); foreach ($lines as $line_num => $line) { if ($length != 0) { if ($line_num >= $length) { break; } } echo $line; } } } The problem is when I have variables or any other php logic in my 'news.php' file, I can't seem to echo it properly. So if I call getitems('news.php', 2) I want to echo: 10/29/07: I've just finished Titanic and it's been great.<BR><BR> 10/24/07: I've changed our location to San Francisco.<BR><BR> Instead it's doing this: 10/29/07: I've just finished $movie_project_01 and it's been great.<BR><BR> 10/24/07: I've changed our location to $current_location.<BR><BR> Does anyone know how to do this properly? I can't seem to use eval() on it, but perhaps I'm not using it properly. Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted October 30, 2007 Share Posted October 30, 2007 Are those variables global? You haven't declared those variables in your function. Read a little about variable scope it should help you a bit. http://us2.php.net/variables.scope Quote Link to comment Share on other sites More sharing options...
stuntpeople Posted October 30, 2007 Author Share Posted October 30, 2007 I did declare them already. I just omitted that information to make my question more compact. But even if they weren't declared, php would echo an "undefined variable" error, and it doesn't even do that. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 30, 2007 Share Posted October 30, 2007 You'd have to use eval in this situation. Working code: <?php // your vars here // myVars format: // $myVars['variable_name_here'] = 'variable value'; $myVars['movie_project_01'] = 'movie01'; $myVars['current_location'] = '(Unknown Location)'; function getitems($filename, $length) { global $myVars; // grab your variables. if (is_file($filename)) { $lines = file($filename); foreach ($lines as $line_num => $line) { if ($length != 0) { if ($line_num >= $length) { break; } extract($myVars); // get variables out of the array. eval("echo \"$line\";"); } } } } You're best of defining all the variables you'll use in news.php within an array, I called this myVars. That way all you have to do is set $myVars as global rather than all your variables. Quote Link to comment Share on other sites More sharing options...
stuntpeople Posted October 30, 2007 Author Share Posted October 30, 2007 Okay I'll be more specific then. In the 'index.php' page, before the function 'getitems()' is a line, include 'vars.php'; In 'vars.php':. vars.php $movie_project_01 = 'Titanic'; $current_location = 'San Francisco'; Shouldn't these variables be included already? Here's the error I receive when using eval(); c:\program files\easyphp1-8\www\index.php(12) : eval()'d code on line 1 Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted October 31, 2007 Share Posted October 31, 2007 you can use preg_replace consider the following example $test = "best"; $strReplace = 'this has to be replaced $test well'; print preg_replace("/\\\$([\w\d\_0-9]*)\b/e","$$1",$strReplace); Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted October 31, 2007 Share Posted October 31, 2007 Okay I'll be more specific then. In the 'index.php' page, before the function 'getitems()' is a line, include 'vars.php'; In 'vars.php':. vars.php $movie_project_01 = 'Titanic'; $current_location = 'San Francisco'; Shouldn't these variables be included already? Here's the error I receive when using eval(); c:\program files\easyphp1-8\www\index.php(12) : eval()'d code on line 1 That is fine however in your function (getitems) you'll need to define all those variables in vars.php as global. You cannot use variables within a function that is defined outside of it, unless you define it as global (within the function) or your pass that variable as an argument. Functions have there own variable scope. You'll want to read up on variable scrope in order to understand how variables work with functions. Incorrect: $myvar = 'hello world'; function myfunc() { echo $myvar; } myfunc() // produces undefined variable 'myvar' notice Correct $myvar = 'hello world'; function myfunc() { global $myvar; // tell the function myvar is a global variable. echo $myvar; } myfunc() // produces hello world Quote Link to comment Share on other sites More sharing options...
stuntpeople Posted November 6, 2007 Author Share Posted November 6, 2007 I've made a test and using 'global' does make the variable accessible, but using the code eval("echo \"$line\";"); I receive the following error: Parse error: parse error in c:\index.php(14) : eval()'d code on line 1 Quote Link to comment Share on other sites More sharing options...
trq Posted November 7, 2007 Share Posted November 7, 2007 Try... eval("echo $line;"); Quote Link to comment Share on other sites More sharing options...
stuntpeople Posted November 8, 2007 Author Share Posted November 8, 2007 That returns Parse error: parse error, expecting `','' or `';'' in c:\program files\easyphp1-8\www\main.php(19) : eval()'d code on line 1 Quote Link to comment Share on other sites More sharing options...
stuntpeople Posted November 8, 2007 Author Share Posted November 8, 2007 I fixed it. In my external file, I had to put single quotes around the strings I was reading in. Thanks everyone! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.