d34fdumbbl1nd |
26.06.12 17:17 |
Zitat:
Zitat von ludwiger
(Beitrag 23629464)
Mein Problem besteht im explode() bzw es nach der Rechnung wieder in einen String zusammenzusetzen...
|
vom explode ist im quelltext gar nichts zu sehen. explode gibt eine array liste zurück. somit musst du entweder jedes element einzeln behandeln oder im anschluss zusammen rechnen.
wie auch immer, kurze erläuterung.
ein textarea enthält somit zeilenumbrüche. später kann jeder string allerdings immer noch leerzeilen und der gleichen enthalten. die evtle fehler produzieren oder sich unerwartet verhalten. hier drei möglichkeiten wie du die zeichenketten wieder verbinden kannst.
also
Code:
<?php
$new_line_separator = "\n";
$input = '15
20
25
30
35
40';
$array = explode( $new_line_separator, $input );
$output_array = array();
$output_array_push = array();
$output_string = (string) '';
$output_string_2 = (string) '';
foreach( $array as $k => $v )
{
$math = trim( $v ) / 29.97 * 23.976;
$output_string .= $math; // string anhängen
$output_string .= ( $k < count( $array ) -1 ) ? $new_line_separator : ''; // zeilenumbruch anhängen
$output_string_2 = $output_string_2 . $math; // so wäre es beispielsweise auch in javas*****
if( $k < count( $array ) -1 )
{
$output_string_2 = $output_string_2 . $new_line_separator;
}
$output_array[] = $math; // wert einer liste hinzufügen
$output_array_push_count = array_push( $output_array_push, $math ); // wert einer liste hinzufügen
}
$output_array_string = (string) implode( $new_line_separator, $output_array ); // das array zu einem string zusammenführen
$output = array
(
'output_string' ,
'output_string_2' ,
'output_array_string' ,
'output_array' ,
'output_array_push' ,
'output_array_push_count' ,
);
// ausgabe
print ( '<pre>' );
foreach( $output as $out_var )
{
if( !empty( $$out_var ) ) // $$ variable variablen
{
print ( '<b>' . $out_var . '</b>' );
if( is_array( $$out_var ) )
{
print ( ' Array(' . count( $$out_var ) . ')' );
}
else if( is_string( $$out_var ) )
{
print ( ' String(' . strlen( $$out_var ) . ')' );
}
print ( $new_line_separator );
print_r( $$out_var );
print ( str_repeat( $new_line_separator, 2 ) );
}
}
print ( '</pre>' );
?>
der oben stehende code gibt folgendes an den browser
Code:
output_string String(17)
12
16
20
24
28
32
output_string_2 String(17)
12
16
20
24
28
32
output_array_string String(17)
12
16
20
24
28
32
output_array Array(6)
Array
(
[0] => 12
[1] => 16
[2] => 20
[3] => 24
[4] => 28
[5] => 32
)
output_array_push Array(6)
Array
(
[0] => 12
[1] => 16
[2] => 20
[3] => 24
[4] => 28
[5] => 32
)
output_array_push_count
6
das zu deiner frage.
hier mein vorschlag. die php datei sieht dann wie folgt im browser aus (inklusive stylesheet, javas***** zur zentrierung, pfeil grafik -base64 encodiert). getest im firefox 13.0.1 und chrome 19.0.1084.56 m
das input feld habe is per css ausgeblendet, da der ie bei javas***** forumal manipulation sonst rum machen könnte.
du kannst es wie folgt aufrufen
entweder per get übermittelung in einer List
// .php?input[]=10&input[]=20&input[]=30&input[]=40&input[]=50&input[]=60&input[]=70&input[]=80&input[]=90
in einem komma separiertem string
// .php?input=10,20,30,40,50,60,70,80,90,100
nur eine zahl
// .php?input=100
oder in einem string nach frei verwendbarem separator
// .php?input=100;60&separator=;
wenn man jetzt noch lustig wäre könnte man das ganze auch noch komplett mit javas***** zum laufen kriegen. sprich: php falls javas***** aus ist und ansonsten nur per javas*****.
[IMG]http://i1122.photobucket.com/albums/l524/deafdumpblind/screenshots/php_html_javas*****_umberechnung_von_ntsc_zu_pal.p ng[/IMG]
Code:
<?php
// ÜTF-8 ÉNCÖDÈD
error_reporting(E_ALL);
$input_content = (string) '';
$calc_content = (string) '';
$output_content = (string) '';
$search_replace_table = array
(
'__body_background_color__' => 'silver' ,
'__body_font_family__' => 'monospace' ,
'__container_id__' => 'container' ,
'__container_height__' => '550' ,
'__container_width__' => '470' ,
'__container_padding_top__' => '2em' ,
'__textarea_height__' => '500' ,
'__textarea_width__' => '100' ,
'__textarea_border__' => '1px solid gray' ,
'__label_font_color__' => 'white' ,
'__form_name__' => 'ntsc2pal' ,
'__form_action__' => basename( __FILE__ ) ,
'__form_method__' => 'POST' ,
'__input_id__' => 'cell_ntsc' ,
'__input_headline__' => 'ntsc' ,
'__input_title__' => 'input' ,
'__input_submit_id__' => 'submit_berechnen' ,
'__input_submit_value__' => 'berechnen >' ,
'__input_submit_button_width__' => '162' ,
'__input_submit_button_background_color__' => 'rgba( 0, 255, 0, 0.5 )' ,
'__input_submit_button_border__' => '1px solid black' ,
'__input_submit_button_color__' => 'gray' ,
'__input_submit_button_font_weight__' => 'bold' ,
'__input_content__' => $input_content ,
'__input_style__' => 'cursor: text;' , // with ending semicolon
'__calc_id__' => 'cell_calculation' ,
'__calc_headline__' => 'calculation' ,
'__calc_height__' => '471' ,
'__calc_width__' => '140' ,
'__calc_title__' => 'start' ,
'__calc_background_color__' => 'rgba( 0, 255, 0, 0.5 )' ,
'__calc_background_image__' => 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHkAAABXCAYAAAAgXOoPAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYxIDY0LjE0MDk0OSwgMjAxMC8xMi8wNy0xMDo1NzowMSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJ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XH5ZO5QmbAUM14BUUimMcXR0NC8nUIHkORbJ2EKoxRDMct1xiAJMAPWIkQr9JhMuncAgBZygFTCdaIwVJ1A5xKyw48Juee734xRD1MGDRJI43WGQQNNSHXTvTROM+hb0CBLXYKzqCRS+NjUWA/hc2EftojdyJNMeGYPX0YpgWjNNlAL1hIvKmEGgTghV7oNcp44VE0QvmmSAR8JeRiUrCMGhI1klGjIY9hmvpCpcQSRbr4CFGSt2FLQFQztBl4oEMCfsNSfkH0Y5UKvVUOeMnRXBcUG2VYzIuCaj90nYC42ljAUp3BcRf8u9742oDVTYh7kGfhsBHu15EKcRfoMiv8BvIXg+LsEcyfnEP8LeEvazqQaZ5HzhruPz5CXLdfHledI0wRzJlsozk1wCeWa5zg++ldnz/aRvhEjGezRvl9TReALiywzkGf6+ntYNQfITOavKiMMpk4yXx0K9dspyXSx8L+xk2gRz4pUOsBxOS5IzAZOcLO5Jea5n2QmW6+RwXcpzPeuOMMnJyDOy59dlJp05WK7NAnteFDfu5qlTHMnmgLLk8bwRzCSblecLeZFnlusSyDNHcgnkmUmOD5SB382zPLNcx8MDJ+U/98ORnC5uSHm+U7SOM8nB5Bl/w+kVp/EMdOHAcu0NvI6C11LmijwIjuT2+E3K81zRB8Ik7wfeFMQ7R3hz8JENA2K5bsZDKc+zNg2KI/l/3JLyPGvbwJjkhjzjjX383o0NGwdYdrnG9uhFG6NXxX8CDAAwPNEHugPpIwAAAABJRU5ErkJggg==)' ,
'__calc_background_position__' => 'center center' ,
'__calc_background_repeat__' => 'no-repeat' ,
// to exchange the calculation modifier - at least one more line must be modified
'__calc_content__' => '/ 29.97 * 23.976' , // search for calculation
'__calc_style__' => 'cursor: default;' , // with ending semicolon
'__calc_cursor_js__' => 'pointer' ,
'__output_id__' => 'cell_output' ,
'__output_headline__' => 'pal' ,
'__output_title__' => 'output' ,
'__output_content__' => $output_content ,
'__output_style__' => 'cursor: default;' , // with ending semicolon
);
// call
// .php?input[]=10&input[]=20&input[]=30&input[]=40&input[]=50&input[]=60&input[]=70&input[]=80&input[]=90
// .php?input=10,20,30,40,50,60,70,80,90,100
// .php?input=100
// .php?input=100;60&separator=;
// $_POST user input if given
$cell_key = $search_replace_table['__input_id__']; // 'cell_ntsc';
// $_GET to override $_POST id given
$input_key = 'input'; // for get method
$input_separator_key = 'separator'; // for get method
$input_separator = ','; // for get method
$headline_input = $search_replace_table['__input_headline__']; // 'ntsc';
$headline_calc = $search_replace_table['__calc_headline__']; // 'calculation';
$headline_output = $search_replace_table['__output_headline__']; // 'pal';
$titles = array();
$title_input = $search_replace_table['__input_title__']; // 'input';
$title_calc = $search_replace_table['__calc_title__']; // 'start';
$title_output = $search_replace_table['__output_title__']; // 'output';
if( !array_key_exists( $cell_key, $_POST ) )
{
if( array_key_exists( $input_key, $_GET ) )
{
$post_array = array();
if( is_array( $_GET[$input_key] ) )
{
$input_array = $_GET[$input_key];
}
if( is_string( $_GET[$input_key] ) )
{
if( array_key_exists( $input_separator_key, $_GET ) )
{
$input_separator = $_GET[$input_separator_key];
}
$input_separator_found = strpos( $_GET[$input_key], $input_separator );
if( $input_separator_found !== false )
{
$input_array = explode( $input_separator, $_GET[$input_key] );
}
else
{
$input_array = array( trim( $_GET[$input_key] ) );
}
}
if( is_array( $input_array ) )
{
foreach( $input_array as $get_input_key => $get_input_value )
{
$post_array[] = trim( $get_input_value );
}
}
if( count( $post_array ) > 0 )
{
$_POST[$cell_key] = implode( "\n", $post_array );
}
}
else
{
$_POST[$cell_key] = implode(
"\n",
array(
'25' ,
'50' ,
'75' ,
'100' ,
'125' ,
'150'
)
);
}
}
if( array_key_exists( $cell_key, $_POST ) )
{
$line_separator = "\n";
$celltime_ntsc = array();
$celltime_ntsc = explode( $line_separator, $_POST[$cell_key] );
$result_string = (string) '';
$result_array = array();
if( count( $celltime_ntsc ) > 0 ) // expecting at least one element in the array
{
foreach( $celltime_ntsc as $celltime_key => $celltime_value ) //
{
$celltime_value = trim( $celltime_value ); // remove spaces and new lines
if( is_numeric( $celltime_value ) ) //
{
$result_array[] = ceil( $celltime_value / 29.97 * 23.976 ); // calculation
}
}
}
if( count( $result_array ) > 0 )
{
$result_string = implode( "\n", $result_array );
}
}
$search_replace_table['__input_content__'] = $_POST[$cell_key];
$search_replace_table['__output_content__'] = $result_string;
// HTML MINI TEMPLATE START
// $search_replace_table on top of this document
ob_start( 'ob_output' );
?><!DOCTYPE html>
<html>
<head>
<title>umrechnung von ntsc auf pal</title>
<meta charset="UTF-8" />
<style type="text/css">
body
{
height: 100%;
width: 100%;
margin: 0;
padding: 0;
background-color: __body_background_color__;
font-family: __body_font_family__;
}
textarea
{
margin: 0;
padding: 10px;
border: __textarea_border__;
width: __textarea_width__px;
height: __textarea_height__px;
}
input#__input_submit_id__ /* submit button */
{
width: __input_submit_button_width__px;
display: block;
background-color: __input_submit_button_background_color__;
border: __input_submit_button_border__;
color: __input_submit_button_color__;
font-weight: __input_submit_button_font_weight__;
cursor: pointer;
}
textarea#__calc_id__
{
width: __calc_width__px;
height: __calc_height__px;
margin-top: 5px;
}
label
{
font-size: 1.5em;
font-weight: bold;
text-align: center;
display: block;
color: __label_font_color__;
}
.float-left
{
float: left;
}
.left-space
{
margin-left: 2em;
}
.pointer ,
label
{
cursor: pointer;
}
#__container_id__
{
width: __container_width__px;
margin: auto;
padding-top: __container_padding_top__;
}
</style>
<s*****>
function recalibrate( id, width, height )
{
if( document.getElementById( id ) )
{
var rootElement = ( document.documentElement && document.compatMode == 'CSS1Compat' ) ? document.documentElement : document.body;
var viewport_width = self.innerWidth ? self.innerWidth : rootElement.clientWidth; // viewport width
var viewport_height = self.innerHeight ? self.innerHeight : rootElement.clientHeight; // viewport height
var element = document.getElementById( id );
var element_width = width;
var element_height = height;
element.style.position = 'absolute';
element.style.left = ( ( viewport_width - element_width ) / 2 ) + 'px';
element.style.top = ( rootElement.scrollTop + ( viewport_height - element_height ) /2 ) + 'px';
}
return
}
/*
functions addEvent & removeEvent from
http://ejohn.org/projects/flexible-javas*****-events/
oncload call of addeven
http://www.webmonkey.com/2010/02/javas*****_events/
*/
function addEvent( obj, type, fn )
{
if ( obj.attachEvent )
{
obj['e'+type+fn] = fn;
obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
obj.attachEvent( 'on'+type, obj[type+fn] );
}
else
{
obj.addEventListener( type, fn, false );
}
return
}
function removeEvent( obj, type, fn )
{
if ( obj.detachEvent )
{
obj.detachEvent( 'on'+type, obj[type+fn] );
obj[type+fn] = null;
}
else
{
obj.removeEventListener( type, fn, false );
}
return
}
function activate()
{
document.getElementById( '__container_id__' ).style.paddingTop = '0px';
document.getElementById( '__input_submit_id__' ).style.display = 'none';
document.getElementById( '__calc_id__' ).style.height = '__textarea_height__'+'px';
document.getElementById( '__calc_id__' ).style.marginTop = '0'+'px';
document.getElementById( '__calc_id__' ).style.cursor = '__calc_cursor_js__';
// recalibrate( id, width, heigth )
// also modify css
// container-width
// container-height
addEvent
(
window, 'resize',
function( event )
{
recalibrate( '__container_id__', __container_width__, __container_height__ );
}
);
recalibrate( '__container_id__', __container_width__, __container_height__ );
document.getElementById( '__calc_id__' ).style.backgroundColor = '__calc_background_color__';
document.getElementById( '__calc_id__' ).style.backgroundImage = '__calc_background_image__';
document.getElementById( '__calc_id__' ).style.backgroundPosition = '__calc_background_position__';
document.getElementById( '__calc_id__' ).style.backgroundRepeat = '__calc_background_repeat__';
return
}
</s*****>
</head>
<body onload="activate();">
<div id="__container_id__">
<form name="__form_name__" action="__form_action__" method="__form_method__">
<div class="float-left">
<label for="__input_id__" title="__input_title__">__input_headline__</label>
<textarea name="__input_id__" id="__input_id__" title="__input_title__" style="__input_style__">__input_content__</textarea>
</div>
<div class="float-left left-space pointer" onclick="document.ntsc2pal.submit();" style="cursor: pointer;">
<label for="__calc_id__" title="__calc_title__">__calc_headline__</label>
<input type="submit" value="__input_submit_value__" id="__input_submit_id__">
<textarea id="__calc_id__" readonly="readonly" title="__calc_title__" style="__calc_style__">__calc_content__</textarea>
</div>
</form>
<div class="float-left left-space">
<label for="__output_id__" title="__output_title__">__output_headline__</label>
<textarea id="__output_id__" readonly="readonly" title="__output_title__" style="__output_style__">__output_content__</textarea>
</div>
</div>
</body>
</html><?php
ob_end_flush();
// HTML MINI TEMPLATE STOP
function ob_output( $buffer )
{
global $search_replace_table; // on top of this document
return
(
str_replace
(
array_keys( $search_replace_table ) ,
array_values( $search_replace_table ) ,
$buffer
)
);
}
?>
|