0

現在、問題があります。

正規表現を使用してテキストのブロックをフォーマットしようとしています。これまでに得たものを説明し、次に問題を説明します。

いくつかの物語のテキストを含むテキスト ファイルがあります。

VOLUME I



CHAPTER I


Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type 

It was popularised in the 1960s with the release of Letraset sheets containing 
Lorem Ipsum passages, and more recently with desktop publishing software like 
Aldus PageMaker including versions of Lorem Ipsum.


VOLUME II



CHAPTER II


Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
It has survived not only five centuries, but also the leap into electronic 
typesetting, remaining essentially unchanged. 

It was popularised in the 1960s with the release of Letraset sheets 
containing Lorem Ipsum passages, and more recently with desktop 
publishing software like Aldus PageMaker including versions of Lorem Ipsum.

...
...

これには複数のVOLUMESCHAPTERSがあり、テキスト ファイルと同じように適切な間隔で PHP でフォーマットする必要があります。

まず、この書式設定関数を呼び出して、空白とクリーンアップを処理します。

<?php    
function formatting($AStr)
{
    return preg_split('/[\r\n]{2,}/', trim($AStr));        
}    
?>

次に、ファイルを呼び出して、引き続きフォーマットを試みます。

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>
<body>

<h1>Jane Austen</h1>

<h2>Emma</h2>

<?php

require_once 'format.inc.php';

$p = file_get_contents('emma.txt');

$p = formatting($p);

/*
foreach ($p as $l) {
    $l = trim($l);
    preg_replace('/(VOLUME +[IVX]+)/', "jjj", $l);
    $volumePattern = '/(VOLUME +[IVX]+)/';
    $chaperPattern = '/(CHAPTER +[IVX]+)/';
    $l = str_replace("\r\n", ' ', $l);

    if (preg_match('/(VOLUME +[IVX]+)/', $l, $m)) {
        echo '<h3>' . $m[1] . '</h3>';
    }
    if (preg_match('/(CHAPTER +[IVX]+)/', $l, $m)) {
        echo '<h3>' . $m[1] . '</h3>';
    }
    preg_replace('/(VOLUME +[IVX]+)/', "jjj", $l);
    echo $l . "\n";
}*/

foreach ($p as $l) {
    //$l = trim($l);
    //$l = str_replace("[\r\n]", '\n', $l);
    if (preg_match('/[\.\w]/', $l, $m)) {
        echo "\n";
    }
    if (preg_match('/(VOLUME +[IVX]+)/', $l, $m)) {
        echo '<h3>' . $m[1] . '</h3>';
    }
    $l = preg_replace('/(VOLUME +[IVX]+)/', '', $l);
    if (preg_match('/(CHAPTER +[IVX]+)/', $l, $m)) {
        echo '<h3>' . $m[1] . '</h3>';
    }
    $l = preg_replace('/(CHAPTER +[IVX]+)/', '', $l);
    echo $l . "\n";
}


?>

</body>
</html>

問題は、印刷する各段落間の空白 (改行) を取得できないことです。試しましたが、できません。私はこの行を使って試しました:

if (preg_match('/[\.\w]/', $l, $m)) {
            echo "\n";
        }
4

2 に答える 2

3

これは非常に単純化しすぎているかもしれませんが、これだけではできませんか?

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>
<body>

<h1>AUTHOR NAME</h1>

<h2>TITLE</h2>

<?php

  $p = file_get_contents('emma.txt');
  echo preg_replace('/^\s*((?:VOLUME|CHAPTER)\s+[IVX]+)\s*$/im', '<h3>$1</h3>', $p); 

?>

</body>
</html>

編集

本文の段落も折り返すには<p></p>(段落に改行がない場合)、次のようにします。

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>
<body>

<h1>AUTHOR NAME</h1>

<h2>TITLE</h2>

<?php

  $p = file_get_contents('emma.txt');
  echo preg_replace_callback('/^\s*(?:(?P<header>(?:VOLUME|CHAPTER)\s+[IVX]+)|(?P<body>.+))\s*$/im', function($matches) {
    if (!empty($matches['body'])) {
      return '<p>'.htmlspecialchars($matches['body']).'</p>';
    } else {
      return '<h3>'.htmlspecialchars($matches['header']).'</h3>';
    }
  }, $p);

?>

</body>
</html>

動いているのを見る

于 2012-08-24T10:22:42.970 に答える
1

さまざまなエラーがあります。最初に「フォーマット」関数で正規表現を次のようにする必要があります。

function formatting($AStr)
{
    return preg_split('/[\r\n]{2,}/', trim($AStr));        
}

preg_replace には参照渡しの変数がないことを知っておく必要があるため、関数の戻り値で行を置き換える必要があります。

foreach ($p as $l) {
    $l = trim($l);
    preg_replace('#VOLUME\s+[A-z]+#Ui', "jjj", $l);
    $l = str_replace("\r\n", ' ', $l);
    if (preg_match('/(VOLUME +[IVX]+)/', $l, $m)) {
        echo '<h3>' . $m[1] . '</h3>';
    }
    $l = preg_replace('/(VOLUME +[IVX]+)/', '', $l);
    if (preg_match('/(CHAPTER +[IVX]+)/', $l, $m)) {
        echo '<h3>' . $m[1] . '</h3>';
    }
    $l = preg_replace('/(CHAPTER +[IVX]+)/', '', $l);
    echo $l . "\n";
}
于 2012-08-24T10:27:30.113 に答える