2012-04-08 9 views
0

[quote][/quote]で囲まれたテキストがあります。これらのタグの前にあるすべてのテキスト、これらのタグの間のすべて、およびこれらのタグの後ろのすべてを照合しようとしています。キャッチとは、それらが複数存在する可能性があるが、互いに存在しないことである。preg_match_all奇妙なこと

私がこれを行うのは、複数の出現の有無にかかわらず、それらのタグの外のすべてのテキストに対してフィルタを実行したいからです。

これは、私が一緒に仕事し始めているものです:

preg_match_all("/(^.*)\[quote\](.*?)\[\/quote\](.*)/si", $reply['msg'], $getthequotes); 

ここでは出力です:

Array 
(
[0] => Array 
    (
     [0] => putting some stuff before the quote 
[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

yep 

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 

adding a quote 

[quote][b]Logan said[/b][br]This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

[i]04/07/12 20:18:07: Edited by Logan(2)[/i] 
    ) 

[1] => Array 
    (
     [0] => putting some stuff before the quote 

[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

yep 

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 

adding a quote 


    ) 

[2] => Array 
    (
     [0] => [b]Logan said[/b][br]This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i] 
    ) 

[3] => Array 
    (
     [0] => 

[i]04/07/12 20:18:07: Edited by Logan(2)[/i] 
    ) 

) 

あなたはそれが所望の出力を得ていない見ることができるように。どんな助けもありがとう。

+0

ああ... HTMLではないマークアップ言語 - 確かに最終的には正しいツールとなるでしょう正規表現しますか? –

+0

私はhtmlに解析されるタグのようなカスタムbbcodeを持っています。すべての正規表現解析はPHPで行われます。 –

+1

申し訳ありませんが、私はこの[非常に一般的な誤り](http://stackoverflow.com/a/1732454/596781)に照らしてちょっと皮肉っていました。答えは正しいツールではないので、正規表現を使ってはいけません。 –

答えて

1

私はこれを試していないが、しかし、あなたは唯一の[quote]前のものにしたいと[/quote]後、あなたはオープニング引用タグの最初の出現のためにstrposを行うことができます。今では、前にすべてが引用されていないことを知っています。

次に、最初に一致する見積もりタグのインデックスから始まるstrposを使用して、終了見積もりタグを見つけることができます。あなたはこのものを捨てることができます。

今すぐ見つけた閉じ引用タグの開始位置を使用して、次の引用ブロックの別のstrposを実行します。あなたは終わりまでこれを繰り返すことができます。

+0

また、入れ子にしたい場合は、最初の '[/ quote]'を最初に検索し、そこから '* quote 'を検索する* backwards *を検索してください。必要に応じてフォーマットし、すすぎ、繰り返します。 – mpen

+0

私はそれをすべて必要とします。私は引用符で囲まれていないテキストに対して余分な処理をするだけです。私はこれを行うことができたと思うが、それをそれぞれの変数に保存してから、すべての部分を再びつなぎ合わせる...逆さまになる。しかし、うまくいくと思う。 –

+0

はい、部品をつなぎ合わせるとうまくいくはずです。申し訳ありません。うん、これは単純なアルゴリズムですが、あなたの目的にはそれほど遅くすべきではありません。私は、実際にUdacity 101クラスからこのアイデアを得たと思います。そこでは、同様のアプローチを使用してHTMLページのリンクを解析しました。 – Gohn67

0

これは可能ですが、文字列に複数のパスを設定する必要があります。

$string = 'putting some stuff before the quote 
[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

yep 

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 

adding a quote 

[quote][b]Logan said[/b][br]This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

[i]04/07/12 20:18:07: Edited by Logan(2)[/i]putting some stuff before the quote 

[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote] 

yep 

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 

adding a quote'; 

//get rid of whitespace 
$string = preg_replace('%\s\s?%', " ",$string); 
//break the string on a common element 
$pieces = preg_split('%\[%',$string); 
//now discard the elements that are tags 
foreach($pieces as $key=>$value): 
    $value = trim($value); 
    if(strrpos($value,"]") == (strlen($value) -1)): 
     unset($pieces[$key]); 
    endif; 
endforeach; 
print_r($pieces); 
//and finally strip out the tag fragments 
foreach($pieces as $key=>$value): 
    $pieces[$key] = preg_replace('%.*]%',"",$value); 
endforeach; 

結果は次のようになり、配列:

Array 
(
    [0] => putting some stuff before the quote 
    [2] => Logan said 
    [4] => testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 
    [6] => did it work? 
    [9] => 04/04/12 23:48:46: Edited by Logan(2) 
    [13] => 04/04/12 23:55:44: Edited by Logan(2) 
    [15] => yep http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA adding a quote 
    [17] => Logan said 
    [19] => This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 
    [21] => did it work? 
    [24] => 04/04/12 23:48:46: Edited by Logan(2) 
    [28] => 04/04/12 23:55:44: Edited by Logan(2) 
    [31] => 04/07/12 20:18:07: Edited by Logan(2) 
    [32] => putting some stuff before the quote 
    [34] => Logan said 
    [36] => testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA 
    [38] => did it work? 
    [41] => 04/04/12 23:48:46: Edited by Logan(2) 
    [45] => 04/04/12 23:55:44: Edited by Logan(2) 
    [47] => yep http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA adding a quote 
)