0

特定のスタイルでフォーマットしたい文字列 (テキスト、数字、句読点など) があります。

私はこれを持っています:

1. TOC 1.1 Chapter 1.1 1.2 Chapter 1.2 1.3 Chapter 1.3 2. Automation 2.1 This is Chapter 2.1 2.2 Chapter 2.2 is it's name 3. Mechanics 3.1 Chapter 3.2 Some Chapter 3.2a Sub-chapter of 3.2 3.2b Sub-chapter of 3.2 chapter

これが必要です:

  1. TOC
    1.1 Chapter 1.1
    1.2 Chapter 1.2
    1.3 Chapter 1.3

  2. Automation
    2.1 This is Chapter 2.1
    2.2 Chapter 2.2 is it's name

  3. Mechanics
    3.1 Chapter 
    3.2 Some Chapter 
        3.2a Sub-chapter of 3.2
        3.2b Sub-chapter of 3.2 chapter

これを試しました:

$text = preg_replace('/([0-9])/', "\n\t$1", $text);

これを取得しました(間違った出力):

1. TOC

1.
1 Chapter 
1.
1

1.
2 Chapter 
1.
2

1.
3 Chapter 
1.
3


2. Automation

2.
1 This is Chapter 
2.
1

2.
2 Chapter 
2.
2 is it's name


3. Mechanics

3.
1 Chapter 

3.
2 Some Chapter 

3.
2a Sub-chapter of 
3.
2

3.
2b Sub-chapter of 
3.
2 chapter

出力が必要な形式で表示されるようにするには、どうすれば修正できますか?

4

1 に答える 1

0

これをブラウザで表示していますか?標準テキストの空白文字はすべてスペースとして扱われ、繰り返される空白文字は 1 つの表示文字にまとめられます。

同様に、正規表現はこれには機能しません。単一の数字を探し、その前に改行/タブを付けます。数字パターン全体を探す必要があります。/(\d\.\d[a-z]?)/

于 2012-08-11T18:05:59.060 に答える