Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Parsing string

  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parsing string

    How would you parse a string that contains php?


    Example:

    $string_to_parse = "<?php print \"foo\";?>";
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    depends what you mean by parse .. parse for something ? or let PHP parse the string, if the latter look at 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...

    PHP Code:
    <?
    eval('?>'.$string_to_parse.'<?');
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #4
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.



    Code:
    <?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;
    	}
    ?>
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone?
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    whats the output of <?echo $thread_to_print ;?> ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #8
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #9
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    whats the output of <?echo $thread_to_print ;?> ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #10
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    whats the output of <?echo $thread_to_print ;?> ?


    The contents I needed to display, and unparsed PHP.
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #11
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #12
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    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.'<?'); ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #13
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums

  • #14
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You're not making any sense to me.

    Just post the code you are trying to eval.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #15
    Regular Coder
    Join Date
    Mar 2004
    Location
    Jackson, Georgia
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    eval('?>'.$thread_to_print.'<?');

    That fixed my problem. Thank you a lot :-D.
    Designer's Toolz; Web & software development community.

    Get your coding questions answered:
    DTZ Forums


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •