我有输入文本区域,提交按钮和输出文本区域。
基本上,当文本输入到inputTextarea时,然后在提交后它将自己发送到OutputTextarea。
我想读取两个textareas的字符串长度。
为什么,我可以读取第一个,因为它的数据相同?
这是捕获,我将文本输入到第一个字段,我有一些逻辑,它将所述数据转换为其他内容,因此我需要一些统计数据进行比较。
这是我到目前为止所尝试的内容:
<head>
<title>Compression</title>
<meta charset="utf-8">
<style type="text/css"></style>
<link rel="stylesheet" type="text/css" href="CompressionStyle.css">
<!-- VALUE DECLARATION -->
<?php
$BinaryCodeInput = $_GET['InputBinaryCode'];
$CompressedCodeOutput = $_GET['OutputCompressedCode'];
$digits = str_split($BinaryCodeInput, 4);
$replace = array(
"" => "",
" " => "",
"1" => "x",
"0" => "y",
"00" => "Q",
"11" => "R",
"01" => "S",
"10" => "T",
"000" => "U",
"001" => "V",
"010" => "W",
"100" => "X",
"111" => "Y",
"110" => "Z",
"101" => "s",
"011" => "z",
"1111" => "A",
"1110" => "B",
"1101" => "C",
"1011" => "D",
"0111" => "E",
"0110" => "F",
"1001" => "G",
"0101" => "H",
"1010" => "I",
"0011" => "J",
"1100" => "K",
"1000" => "L",
"0100" => "M",
"0010" => "N",
"0001" => "O",
"0000" => "P"
);
foreach ($digits as $val) {
//str_replace(' ', '', $digits);
$replace[$val];
}
$CompressedOutputLength = strlen($CompressedCodeOutput);
?>
</head>
<body>
<div class="formOne">
<!-- //INPUT GOES HERE -->
<form action="#" method="GET">
<!-- LABEL FOR BINARY CODE INPUT -->
<label id="Lenter">Enter Binary Code</label>
<!-- INPUT AREA -->
<textarea cols="54" rows="5" name="InputBinaryCode" style="border:dotted 4px black"></textarea>
<!-- SUBMIT BUTTON -->
<input type="submit" value="Compress">
<br>
<br>
<!-- LABEL FOR COMPRESSION OUTPU -->
<label id="Loutput">Compressed Code</label>
<!-- COMPRESSED TEXT AREA -->
<textarea disabled="yes" cols="54" rows="5" name="OutputCompressedCode" style="border: dotted 4px black"><?php
foreach ($digits as $val) {
//str_replace(' ', '', $digits);
echo $replace[$val];
}
//$CompressedOutputLength = strlen($val);
?></textarea>
<!-- SPACERLINE -->
<hr/>
<!-- STATISTICS SECTION -->
<p>Original Code Length: <?php echo strlen($BinaryCodeInput); ?></p>
<p>Compressed Output Code Length: <?php echo $CompressedOutputLength; ?></p>
</form>
</div>
</body>
正如您所看到的那样有效,但问题是,要从输出源获取数据,我必须按两次提交按钮,因此会丢失输出字段中的当前输出。
答案 0 :(得分:1)
您在整个过程中有以下步骤
以下是基于您的逻辑的修改代码。请注意,我创建了一个新变量$output_val
来存储已处理的值。我计算了长度并存储在此变量的$len_output
中并打印出来。
<head>
<title>Compression</title>
<meta charset="utf-8">
<style type="text/css"></style>
<link rel="stylesheet" type="text/css" href="CompressionStyle.css">
<!-- VALUE DECLARATION -->
<?php
$BinaryCodeInput = $_GET['InputBinaryCode'];
$CompressedCodeOutput = $_GET['OutputCompressedCode'];
$digits = str_split($BinaryCodeInput, 4);
$replace = array(
"" => "",
" " => "",
"1" => "x",
"0" => "y",
"00" => "Q",
"11" => "R",
"01" => "S",
"10" => "T",
"000" => "U",
"001" => "V",
"010" => "W",
"100" => "X",
"111" => "Y",
"110" => "Z",
"101" => "s",
"011" => "z",
"1111" => "A",
"1110" => "B",
"1101" => "C",
"1011" => "D",
"0111" => "E",
"0110" => "F",
"1001" => "G",
"0101" => "H",
"1010" => "I",
"0011" => "J",
"1100" => "K",
"1000" => "L",
"0100" => "M",
"0010" => "N",
"0001" => "O",
"0000" => "P"
);
$output_val = "";
foreach ($digits as $val) {
//str_replace(' ', '', $digits);
$replace[$val];
$output_val = $output_val . $replace[$val];
}
$CompressedOutputLength = strlen($CompressedCodeOutput);
$len_output = strlen($output_val);
?>
</head>
<body>
<div class="formOne">
<!-- //INPUT GOES HERE -->
<form action="#" method="GET">
<!-- LABEL FOR BINARY CODE INPUT -->
<label id="Lenter">Enter Binary Code</label>
<!-- INPUT AREA -->
<textarea cols="54" rows="5" name="InputBinaryCode" style="border:dotted 4px black"></textarea>
<!-- SUBMIT BUTTON -->
<input type="submit" value="Compress">
<br>
<br>
<!-- LABEL FOR COMPRESSION OUTPU -->
<label id="Loutput">Compressed Code</label>
<!-- COMPRESSED TEXT AREA -->
<textarea disabled="yes" cols="54" rows="5" name="OutputCompressedCode" style="border: dotted 4px black"><?php
foreach ($digits as $val) {
//str_replace(' ', '', $digits);
echo $replace[$val];
}
//$CompressedOutputLength = strlen($val);
?></textarea>
<!-- SPACERLINE -->
<hr/>
<?php echo $output_val; ?>
<!-- STATISTICS SECTION -->
<p>Original Code Length: <?php echo strlen($BinaryCodeInput); ?></p>
<p>Compressed Output Code Length: <?php echo $len_output; ?></p>
</form>
</div>
在这段代码中,我在输入文本值中添加了20(为了理解我只输入数字)作为逻辑的替代。之后显示$output_new
及其长度。
作为一个简单的规则,使用来自它们的位置而不是输出字段的值始终是一个好主意。在您的情况下,我已将输出值存储在变量中并在需要的地方使用它,即在Output Textarea中输出并计算长度。
答案 1 :(得分:0)
strlen()将在空值上返回NULL,因此对于最后一行,将其更改为:
<p>Output String Length: <?php echo (strlen($OutputText)) ? strlen($OutputText) : '0'; ?> </p>
如果没有设置值,这将使回退为零。