Sign in to follow this  
dead1ock

[web] ob_get_clean() problems.

Recommended Posts

dead1ock    102
I am trying to create a function that grabs a script file and executes the output on a telnet device. I have it working, but ob_get_clean() seems to be removing any newlines after a php closing brace ( i.e. ?> ). Has anyone encountered this problem?

PHP
[code]

public final function execScript($name, $args)
{
ob_start();
include("../apps/frontend/modules/device/scripts/" . $name . ".php");
$partial = ob_get_clean();
$commands = explode("\n", $partial);

foreach($commands as $command)
{
$output .= $this->telnet->exec($command);
}

return $output;
}
[/code]

"Script"
[code]

conf
int ethernet 1/<?php echo $args['port']; ?>
switchport allowed vlan add <?php echo $args['vlan_id']; ?> tagged
switchport native vlan <?php echo $args['vlan_id']; ?>
switchport allowed vlan remove 1
end[/code]

Expected Output
[code]

conf
int ethernet 1/18
switchport allowed vlan add 100 tagged
switchport native vlan 100
switchport allowed vlan remove 1
end
[/code]

Actual Output
[code]

conf
int ethernet 1/18switchport allowed vlan add 100 tagged
switchport native vlan 100switchport allowed vlan remove 1
end
[/code]

Share this post


Link to post
Share on other sites
benryves    1999
This is not a problem with ob_get_clean but a "feature" to prevent files containing only PHP code from sending an accidental newline to the browser at the end of the file. According to the [url=http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php]manual[/url]:

[quote]The closing tag for the block will include the immediately trailing newline if one is present.[/quote]

You will need to insert an additional newline to compensate for this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this