Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with eval() :( 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR

Hi guys,

I have a problem with eval() :(

It returns "parse error, unexpected T_STRING in...) : eval()'d code"

Any idea about what's wrong?

Thanks! :)

Code:
function hd_string_table_to_text($arg_imp) {

$arg["labels"]["data"] = array();
$arg["labels"]["align"] = "BOTH"; // LEFT / RIGHT / BOTH
$arg["cells"]["data"] = array();
$arg["cells"]["align"] = "LEFT"; // LEFT / RIGHT / BOTH
$arg["col"]["char"] = "|";
$arg["col"]["padding"] = 0;
$arg["row"]["char"] = "-";
$arg["row"]["padding"] = 0;

$arg = array_merge($arg, $arg_imp);

    // - ! - get max length
    foreach ($arg["labels"]["data"] as $label) {
    
    $strlen_array[] = strlen($label);
    
    }
    
    foreach ($arg["cells"]["data"] as $cell) {
    
    $strlen_array[] = strlen($cell);
    
    }    

rsort($strlen_array);
$max_length = $strlen_array[0];
$max_length += $arg["col"]["padding"];

$header = $arg["col"]["char"];

    // - ! - build header
    foreach ($arg["labels"]["data"] as $label) {
    
    
    $str_pad = "str_pad(" . $label . ", " . $max_length . ", ' ', STR_PAD_" . $arg["labels"]["align"]  . ");"; [b]// PROBLEM HERE!
    [/b]
    
    $header .=  eval($str_pad);
    $header .= $arg["col"]["char"];
    
    $label_tot++;
    
    }

// - ! - build horizontal line
$line = $arg["col"]["char"] . str_repeat($arg["row"]["char"], (strlen($header) - 2)) . $arg["col"]["char"];

// - ! - build table
$i = 1;

$table .= $line . "\n";
$table .= $header . "\n";
$table .= $line . "\n";
$table .= $arg["col"]["char"];

    foreach ($arg["cells"]["data"] as $cell) {

    $table .= str_pad($cell, $max_length, " ", STR_PAD_BOTH) . $arg["col"]["char"];
    
        if ($i == $label_tot) {
        
        $i = 0;
        
        $table .= "\n";
        $table .= $line . "\n";
        $table .= $arg["col"]["char"];
        
        }
    
    $i++;
    
    }   

return $table;

}
 
not sure that you are using eval correctly (or even why you are using eval...). as you have it, the eval will return NULL even if it worked.

this code will work to return the padded value
Code:
<?php
$str_pad = <<<CMD
str_pad($label,$max_length,' ', STR_PAD_{$arg["labels"]["align"]});
CMD;
eval("echo $str_pad");
?>

I suspect that you will want to tweak the last bit as follows
Code:
$header .= eval ("return $str_pad");
 
Thanks :)

So, it's possible not to use eval?

These don't work :

Code:
$test = "STR_PAD_" . $arg["labels"]["align"];
$header .= str_pad($label, $max_length, " ", $test);

Code:
$header .= str_pad($label, $max_length, " ", STR_PAD_{$arg["labels"]["align"]});
 
I suspect that they all work, really. Just not the way you suspect.

try this instead
Code:
$header .= str_pad($label, $max_length, "[red]&nbsp;[/red]", STR_PAD_{$arg["labels"]["align"]});

remember, html is 'quite' agnostic to whitespace.
 

Nope ... it doesn't work :(

It returns : "Parse error: parse error, unexpected '{' in...
 
remove the curly braces?

the main point i was trying to make was that if you include a hard space (&nbsp;) rather than a soft space, you may find that the padding works in the browser (and i'll wager that it was working in the source code already).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top