|
現在の言語: 日本語 |
|
読み込んだファイルデータのタブの判定処理
|
|
判定処理
|
class test1
{
function test1():void
{
//同一階層にphpが存在していると仮定します
$path = "aaa.html";
$ary = [];
$this->loadFile($ary, $path);
for($i=0; $i<3; $i++)
{
$this->loadFileData($ary, $i);
echo "<hr>";
}
}
private function loadFileData(array $ary, int $kind)
{
$flg = 0;
$data = "";
switch($kind)
{
case 0:
$data = "\\tタブ文字を使用して判定";
break;
case 1:
$data = "タブ入力文字を使用して判定";
break;
case 2:
$data = "Unicode エスケープシーケンスを使用して判定";
break;
}
echo $data.PHP_EOL;
for($i=0; $ijudgeTab(mb_substr($data, $j, 1), $kind) == 1)
{
$count++;
}
}
if($count == 0) continue;
//タブと判定したケースは、該当行インデックスとデータを出力
echo "i:$i tab count:$count data:$data".PHP_EOL;
}
}
//タブ判定処理
//$data:1文字
//$kind:判定種類
//0:\tタブ文字を使用して判定
//1:タブ入力文字を使用して判定
//2:Unicode エスケープシーケンスを使用して判定
private function judgeTab(string $data, $kind) : int
{
$result = 0;
switch($kind)
{
case 0:
if ($data === "\t")
{
$result = 1;
}
break;
case 1:
//タブ文字をダブルコーテーション内に設定して判定
if ($data === " ")
{
$result = 1;
}
break;
case 2:
if ($data === "\u{0009}")
{
$result = 1;
}
break;
}
return $result;
}
private function loadFile(array &$ary, string $path):void
{
$ary = [];
$file = fopen($path, "r");
$tmp="";
while(!feof($file))
{
$ary[]=fgets($file);
}
fclose($file);
}
}
echo "<pre>";
$cls1= new test1();
$cls1->test1();
echo "</pre>";
class test1
{
function test1():void
{
//Assumes PHP exists at the same level
$path = "aaa.html";
$ary = [];
$this->loadFile($ary, $path);
for($i=0; $i<3; $i++)
{
$this->loadFileData($ary, $i);
echo "<hr>";
}
}
private function loadFileData(array $ary, int $kind)
{
$flg = 0;
$data = "";
switch($kind)
{
case 0:
$data = "Test using a tab character";
break;
case 1:
$data = "Test using a tab input character";
break;
case 2:
$data = "Test using a Unicode escape sequence";
break;
}
echo $data.PHP_EOL;
for($i=0; $i < count($ary); $i++)
{
$data = $ary[$i];
$length = mb_strlen($data);
$count = 0;
// Judge each character in the read line.
for($j=0; $j < $length; $j++)
{
// Tab detection process
if($this->judgeTab(mb_substr($data, $j, 1), $kind) == 1)
{
$count++;
}
}
if($count == 0) continue;
// If a tab is detected, output the corresponding line index and data.
echo "i:$i tab count:$count data:$data".PHP_EOL;
}
}
// Tab detection process
//$data: 1 character
//$kind: Judgement type
//0: Judge using a tab character
//1: Judge using a tab input character
//2: Judge using a Unicode escape sequence
private function judgeTab(string $data, $kind) : int
{
$result = 0;
switch($kind)
{
case 0:
if ($data === "\t")
{
$result = 1;
}
break;
case 1:
//Set a tab character in double quotes and check.
if ($data === " ")
{
$result = 1;
}
break;
case 2:
if ($data === "\u{0009}")
{
$result = 1;
}
break;
}
return $result;
}
private function loadFile(array &$ary, string $path):void
{
$ary = [];
$file = fopen($path, "r");
$tmp="";
while(!feof($file))
{
$ary[]=fgets($file);
}
fclose($file);
}
}
echo "<pre>";
$cls1= new test1();
$cls1->test1();
echo "</pre>";
| 著作権情報 |
| ホームページおよびアプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |