2011-02-01 59 views
2

我正在做一個簡單的文本編輯器,我需要處理創建段落。如何從加號開始加一段(+)

段將在WikiDot語法,長話短說,我需要改變:

+ paragraph 1 

更改 < H1>段</H1>

++ subparagraph 1 

更改 < H2 >分段</h2>

這是怎麼回事在PHP中?

+1

段落之間的分隔符是什麼?換行符(\ n)?雙換行符(\ n \ n)? – 2011-02-01 22:06:51

+0

@蠟筆兩條新線 – 2011-02-01 22:15:53

回答

3

爲了擴大對@ CrayonViolent的(在情況下,首先更換interrups第二):

<?php 
    $content = "Hello, world 

+ Big Heading 

++ Smaller heading 

Additional content"; 

    function r($m){ 
    $tag = "h".strlen($m[1]); 
    return "<{$tag}>{$m[2]}</{$tag}>"; 
    } 
    $content = preg_replace_callback('/^(\+{1,6})\s?(.*)$/m','r', $content); 

    echo $content; 
?> 

還增加了m(多行)標誌的正則表達式好一點的匹配,並只會做標頭<h1><h6>

工作示例可以定位here

2
$content = preg_replace ("~^\+\+(.*?)\n\n~",'<h2>$1</h2>',$content); 
$content = preg_replace ("~^\+(.*?)\n\n~",'<h1>$1</h1>',$content);