...

View Full Version : Parsing string



DesignersToolz
04-07-2004, 01:44 AM
How would you parse a string that contains php?


Example:

$string_to_parse = "<?php print \"foo\";?>";

firepages
04-07-2004, 01:48 AM
depends what you mean by parse .. parse for something ? or let PHP parse the string, if the latter look at eval() (http://www.php.net/eval)

note that eval() adds opening and closing tags to its contents , so in your case above you would have to deal with them as well...



<?
eval('?>'.$string_to_parse.'<?');
?>

raf
04-07-2004, 01:50 AM
how do you mean?

$var='more blabla';
echo "blableble $var bldld";

is parsing a string and filling in the variable--> double-quoted strings get parsed.

If you mean more elaborate PHP code (or complete scrips even), then you can use eval(), which can parse the PHP code you pull from a db or file or that you dynamically generated

DesignersToolz
04-07-2004, 01:55 AM
What I'm actually doing is getting file contents and parsing it. I can't use include() because I need only a certain portion of the file and not all of it. Anyway, I tried eval() and got:

Parse error: parse error in /var/www/html/forum/reply.php(158) : eval()'d code on line 1


The file i'm reading the contents from has php and html in it..below is the code I used to get the file contents I needed.





<?php
if (file_exists("/var/www/html/forum/$catagory/posts/$post.php"))
{
$old_thread = file_get_contents("/var/www/html/forum/$catagory/posts/$post.php");
$opening_delimiter = "<!-- STARTMESSAGES -->";
$start_delimiter_position = strpos($old_thread, $opening_delimiter);
$length = $start_delimiter_position;
$thread_to_print = substr($old_thread, $start_delimiter_position, 10000);
$crap = eval("$thread_to_print");
print $crap;
}
?>

DesignersToolz
04-08-2004, 01:17 PM
Anyone?

firepages
04-08-2004, 01:43 PM
whats the output of <?echo $thread_to_print ;?> ?

raf
04-08-2004, 01:44 PM
Firdst off, i'm quite convinced an include might be better suited, even if you only need part of the page --> you could have some if-then 's to select the content or you could split the include file up and include only the parts you need).

And to continue about the actual proble : you need to show us the evaled file since the error is there (probably because the file starts with <?php or some HTML or whatever.) + if you want more and better help, then you need to give more specific info about both your actual, technical, problem and about the real, functional, task you're trying to accomplish (in your own intrest : take the time to inform us in a clear, efficient way about your problem/quest)

DesignersToolz
04-09-2004, 02:34 AM
you need to show us the evaled file since the error is there (probably because the file starts with <?php or some HTML or whatever.) + if you want more and better help, then you need to give more specific info about both your actual, technical, problem and about the real, functional, task you're trying to accomplish (in your own intrest : take the time to inform us in a clear, efficient way about your problem/quest)





Thats quite a problem as this is a message board im building and the content of the file will vary.

firepages
04-09-2004, 05:43 AM
whats the output of <?echo $thread_to_print ;?> ?

DesignersToolz
04-09-2004, 02:55 PM
whats the output of <?echo $thread_to_print ;?> ?



The contents I needed to display, and unparsed PHP.

DesignersToolz
04-09-2004, 02:57 PM
But using eval() I get:

Parse error: parse error in /var/www/html/forum/reply.php(158) : eval()'d code on line 1


What this is for is the reply page. It's supposed to show the posts below the reply box. Just like in this forum. (stores posts in files and not a db) And I can't just include the file as it contains things that I dont want to show.

firepages
04-09-2004, 03:12 PM
not being a mind reader , errr remote computer memory reader even , its impossible to advise ... you are free to remove the stuff you dont want anyone to see if its in anyway sensitive.

but I dont see how else we can help , would need to see the start and finish of $thread_to_print as the very least.... did you try

eval('?>'.$thread_to_print.'<?'); ?

DesignersToolz
04-09-2004, 09:29 PM
you are free to remove the stuff you dont want anyone to see if its in anyway sensitive.



The thing I didnt want showing it a menu, which is at the top of the threads.(post reply and new thread). I didnt want those showing.



did you try

eval('?>'.$thread_to_print.'<?'); ?


Nope, I'll try that and get back.

raf
04-09-2004, 10:07 PM
You're not making any sense to me.

Just post the code you are trying to eval.

DesignersToolz
04-09-2004, 11:00 PM
eval('?>'.$thread_to_print.'<?');


That fixed my problem. Thank you a lot :-D.

DesignersToolz
04-09-2004, 11:04 PM
Err...now another problem arises :( :( :( :( :( :( :(

Whenever someone posts it works fine. But when you go to add a reply using the eval() you told me about and the post has php in it then it won't show anything at all. With no php in a post, it shows the posts.

DesignersToolz
04-11-2004, 01:22 PM
I guess I've been given up on..

raf
04-12-2004, 07:28 AM
Since you refuse to post your code, there is nothing we can do for you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum