Solved

Parsing PHP variable within highlight_string syntax display

Posted on 2009-04-10
6
395 Views
Last Modified: 2012-05-06
Hiya
The code below shows a variable $form_code in the value for $mycode.

$mycode is echoed as syntax for copy and paste and this script is to aventually become a form builder where the output is a dynamic PHP and HTML form. Something on another level to what I've done but I think I can do it.

So far I've only been able to get an error or the actual variable $form_code being displayed on the output. Is it possible to parse $form_code so that the output would end up being...

<form></form>
                         <?php
                        echo "PHP will be displayed";
                  ?><h1>and HTML will be displayed</h1>
//This file is to test how I create some HTML with PHP mixed in then output it for use as normal script
 
	$a = "1";
	$b = "2";
	
	if($a == "1")
	{
		$form_code = "<form>";
	}
	
	if($b == "2")
	{
		$form_code .= "</form>";
	}
	
	//This can be used to make the final display of code on the page
	$mycode='$form_code <?php 
				echo "PHP will be displayed"; 
			?><h1>and HTML will be displayed</h1>'; 
	echo highlight_string($mycode,true);

Open in new window

0
Comment
Question by:Ryan Bayne
  • 3
  • 2
6 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 24116634
look at using <pre> and </pre> around the php code you wish to display. Mush like um THIS page.

Michael
0
 
LVL 9

Expert Comment

by:Mahdii7
ID: 24119013
Single Quotes.

You can't include a PHP variable in single quotes and have it output the contents of the variable. Either append it with concatenation ($var . 'some stuff') or use double quotes ("$var some stuff")

See snippet:
<?php
//This file is to test how I create some HTML with PHP mixed in then output it for use as normal script
 
        $a = "1";
        $b = "2";
        
        if($a == "1")
        {
                $form_code = "<form>";
        }
        
        if($b == "2")
        {
                $form_code .= "</form>";
        }
        
        //This can be used to make the final display of code on the page
        $mycode = $form_code . '<?php 
                                echo "PHP will be displayed"; 
                        ?><h1>and HTML will be displayed</h1>'; 
        echo highlight_string($mycode,true);
		
		?>

Open in new window

0
 
LVL 9

Expert Comment

by:Mahdii7
ID: 24119020
Also, if you didn't want the $form_code to be escaped, you would need to output it separately:
<?php
//This file is to test how I create some HTML with PHP mixed in then output it for use as normal script
 
        $a = "1";
        $b = "2";
        
        if($a == "1")
        {
                $form_code = "<form>";
        }
        
        if($b == "2")
        {
                $form_code .= "</form>";
        }
        
        //This can be used to make the final display of code on the page
        $mycode = '<?php 
                                echo "PHP will be displayed"; 
                        ?><h1>and HTML will be displayed</h1>'; 
        echo $form_code . highlight_string($mycode,true);
		
		?>

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 2

Author Comment

by:Ryan Bayne
ID: 24121828
Good ok that all works for me but I plan to get slighly more complicated and have script thats very mixed with html  and php.

What about this example? The PHP variable in the 2nd if statement shows as blank but I'e not been able to make it be display as PHP.

This is all for a form builder by the way. Using values stored by a wizard type form creatore my script will go through a series of if statements building the form as it goes. It will be a heavy mix of HTML and PHP.

My code here is slowly working towards putting the whole form into a single variable then displaying it for copying. I'm wondering if it would be better to echo each part of the code as the script runs. Outputting each line with echo I just think it would be a slower.

What do you think?
<?php 
        $a = "1";
        $b = "2";
        
        if($a == "1")
        {
                $form_code = "<h1>HTML Only example</h1>";
        }
        
        if($b == "2")
        {
                $form_code .= '<?php echo $php_example;?>';
        }
        
        //This can be used to make the final display of code on the page
        $mycode = $form_code; 
        echo highlight_string($mycode,true);            
?>

Open in new window

0
 
LVL 9

Accepted Solution

by:
Mahdii7 earned 300 total points
ID: 24123769
I get your problem. Unfortunately what you probably need is the exact opposite of "eval()" which doesn't exist in PHP. The best approach may be a class that handles data, to clean up syntax structure. Note the " (double) and ' (single) quotes though - they are still needed!
<?php 
class PHPOut {
	var $file_name;
	var $file_resource;
	
	function PHPOut(){
		$this->file_name = md5(rand()*time());
		if(!$this->file_resource = fopen($this->file_name, "w")){
		 die("<strong>There was an error creating the file. Check the permissions of this script! Script needs '644', directory needs '777'</strong>"); 
		}
	}
	
	function add($data){
		$data .= "\n";
		fwrite($this->file_resource, $data);
	}
	
	function output(){
		highlight_file($this->file_name);
		fclose($this->file_resource);
		unlink($this->file_name);
	}
}
 
 
$something = "Hi mom!";
 
$form = new PHPOut;
$form->add("<strong>");
$form->add('<?php $some_undefined_variable ?>');
$form->add("</strong>");
$form->add($something);
 
$output = $form->output();
?>

Open in new window

0
 
LVL 2

Author Comment

by:Ryan Bayne
ID: 24124017
Ah see now I have not worked a great deal with files but this may be the project to learn it on. I'll end up going with the 2 approaches probably, better lessons to be learned that way.

Thanks for your help and that bit of code it will give me somewhere to start thinking on the file approach.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question