2011-03-29 14 views
1

私は、(質問者が記入された後の人の時間を節約するために)手紙編集システムを作成しています。バグ。長年の短い話ですが、この正規表現がなければ修正するまでに何時間もかかるでしょう。それがあなたの素晴らしい助けを求めているからです!開始タグと終了タグとの改行が一致する正規表現

私たちは、次のものが含まれ、いくつかのテキストを持っている...

"<k>This is the start of the paragraph 

This is some more of the paragraph. 

And some more"; 

私は基本的に開始タグを検索することができ、正規表現を必要とし、「<k>」、そしてまたそれは全体の来る最初の新ライン " \ r \ n "?その内容を変数に挿入してから使用することができます(<k>は削除されましたが、新しい行コード "\ r \ n"が残っています)。

私はPHPを使用しており、テキストは(上記の例のように)MySQLに格納されています。

助けてください!

私はこのバグを修正した後、これらを正しく学ぶことを約束します! :)

答えて

1

あなたが5.3を使用している場合はこのようにクロージャのいくつかの利用を行うことができます。

$text = "<k>This is the start of the paragraph 

This is some more of the paragraph. 

And some more"; 

$matches = array(); 

$text = preg_replace_callback('/<k>(.*)$/m', function($match) use (&$matches){ 
    $matches[] = $match[1]; 
}, $text); 

var_dump($text,$matches); 

出力は次のようになります。私は、複数の<k>のタグがあるかもしれないと仮定している

string ' 

This is some more of the paragraph. 

And some more' (length=52) 
array 
    0 => string 'This is the start of the paragraph' (length=34) 

したがって、タグの後に続くすべてのテキストをmatchesという配列に配置します。さらなる例として

は...以下の入力で:

$text = "<k>This is the start of the paragraph 
Line 2 doesn't have a tag... 
This is some more <k>of the paragraph. 
Line 4 doesn't have a tag... 
<k>And some more"; 

出力は次のようになります。

string ' 
Line 2 doesn't have a tag... 
This is some more 
Line 4 doesn't have a tag... 
' (length=78) 
array 
    0 => string 'This is the start of the paragraph' (length=34) 
    1 => string 'of the paragraph.' (length=17) 
    2 => string 'And some more' (length=13) 
+0

ありがとうJacob :)ただ必要なもの。 – Gordi555

0
/^<k>(\w*\s+)$/ 

はおそらく動作します。

関連する問題