Skip to content

Instantly share code, notes, and snippets.

@PJZ9n
Created March 4, 2020 23:41
Show Gist options
  • Save PJZ9n/2dbc7f8c01195f58f3543db4aec05fb4 to your computer and use it in GitHub Desktop.
Save PJZ9n/2dbc7f8c01195f58f3543db4aec05fb4 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
$rawText = file_get_contents("a.txt");
$arrayText = explode("\r\n", $rawText);
//echo "<h3>Raw Text</h3>";
//echo "<pre>" . $rawText . "</pre>";
$replacedArrayText = [];
foreach ($arrayText as $row => $text) {
var_dump($text);
echo "<br>";
$replacedArrayText[$row] = "\r\n";
if ($row === 0) {
//1行目
$replacedArrayText[$row] .= "<h1>" . $text . "</h1>";
continue;
}
if (preg_match('/(第\d+条)/', $text) === 1) {
//第〇条
$replacedArrayText[$row] .= "<h2>" . $text . "</h2>";
continue;
}
if (preg_match('/( *1\.)/', $text) === 1) {
//リストの開始
$replacedArrayText[$row] .= "<ol>" . $text;
$nestSpace = "";
preg_match('/([ ]*)/', $text, $nestSpace);
for ($i = $row; $i < count($replacedArrayText) - 1; $i++) {
//次の要素
if (preg_match('/(' . $nestSpace . ')/', $replacedArrayText[$i + 1]) === 0) {
//最終要素である
$replacedArrayText[$i] = $replacedArrayText[$i] . "</ol>";
break;
}
//ここわからん
}
continue;
}
if ($text !== "") {
//通常の文字列
//$replacedArrayText[$row] .= "<p>" . $text . "</p>";
$replacedArrayText[$row] = $text;
continue;
}
$replacedArrayText[$row] .= $text . "<br>";
}
//echo "<h3>Replaced Text</h3>";
$result = implode("", $replacedArrayText);
echo '<textarea rows="500" cols="500">' . $result . '</textarea>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment