0

(存在する場合は)ハッシュタグを見つけて、各タグの最初の共起と、その後のテキスト、別のタグが存在する場合は次のタグの前のテキストを取得したいと思います。

*すべてのメッセージ文字列にハッシュタグが付いているわけではありません。

これが私がやろうとしていることです:

スクリプトに送信される可能性のある文字列の例のリスト:

1)$ message='いくつかの新しいものを追加しました';

2)$ message=' #BALANCEXのバランスの取れた動き';

3)$ message =' いくつかのログテキストを変更します#他のいくつかのログテキストを修正します';

$ num = prereg_match_all('@ ????? @'、$ message、$ matches);

これは私が試合から目指している配列構造の結果です:

例1の結果)

(
[0] => Array
    (
        [0] => Added some new stuff
    )
)

例2の結果)

(
[0] => Array
    (
        [0] => balanced movement of X
        [1] => #BALANCE
    )
)

例3の結果)

(
[0] => Array
    (
        [0] => some log text
        [1] => #CHANGE
    )
[1] => Array
    (
        [0] => some other log text
        [1] => #FIX
    )
)

REGEXでまともなドキュメントを見つけようとしている私を狂わせるもの

4

3 に答える 3

0

このコードを試してください:

$msg = '#CHANGE some log text #FIX some other log text';
$msg = preg_replace('/(#[\w]+)(\s+)/', "\n\n$1\n", $msg);

foreach (explode("\n\n", trim($msg)) as $k => $v) {
    $res[$k] = array_reverse(explode("\n", $v));
}

print_r($res);
/*
Array
(
    [0] => Array
        (
            [0] => some log text 
            [1] => #CHANGE
        )

    [1] => Array
        (
            [0] => some other log text
            [1] => #FIX
        )

)
*/
于 2012-06-21T12:59:42.370 に答える
0

すべてのテストケースで次のようなものを試してください。

$messages[] = 'Added some new stuff';
$messages[] = '#BALANCE balanced movement of X';
$messages[] = '#CHANGE some log text #FIX some other log text';

foreach( $messages as $message) {
    preg_match_all( '~(#\w+)?\s*([\w\s]+)~i', $message, $matches);
    // var_dump( $matches);
    echo "Message: " . $message . "\n";
    $count = strlen( $matches[1][0]);
    if( $count == 0) {
        // No hash tags
        echo "No hash tags, so the match string text is: " . $matches[2][0] . "\n";
    } else {
        for( $i = 0; $i < count( $matches[1]); $i++) {
            echo "\t Hash tag $i\n";
            echo "\t\t - Tag: ".$matches[1][$i]." Value: ".$matches[2][$i]."\n";
        }
    }
}

これは以下を出力します:

Message: Added some new stuff
No hash tags, so the match string text is: Added some new stuff

Message: #BALANCE balanced movement of X
     Hash tag 0
         - Tag: #BALANCE Value: balanced movement of X

Message: #CHANGE some log text #FIX some other log text
     Hash tag 0
         - Tag: #CHANGE Value: some log text 
     Hash tag 1
         - Tag: #FIX Value: some other log text

それが機能するのを見る

于 2012-06-21T13:01:10.643 に答える
0

わかった!3時間の戦いの後、BSDNOOBZが最も近い答えを出しました...

次の場合を考えてみましょう。

$messages[] = 'Blla vajfkj asfhkha asfha lskahfa';
$messages[] = '#CHANGE reduced sentry health/armor to 325/100 and made them easier to repair with welders #FIX reduced sentry build time to 6 seconds (down from 10)';
foreach($messages AS $message)
{
    $num = preg_match_all('@(\#\w+)([^#]+)@', $message, $matches, PREG_SET_ORDER);
    if($num > 0)
    {
        print_r($matches);
    } else {
        echo $message;
    }
}

結果:

string Blla vajfkj asfhkha asfha lskahfa

Array ( 
    [0] => Array 
    ( 
        [0] => #CHANGE reduced sentry health/armor to 325/100 and made them easier to repair with welders 
        [1] => #CHANGE 
        [2] => reduced sentry health/armor to 325/100 and made them easier to repair with welders 
    ) 
    [1] => Array 
    ( 
        [0] => #FIX reduced sentry build time to 6 seconds (down from 10) 
        [1] => #FIX 
        [2] => reduced sentry build time to 6 seconds (down from 10) 
    )
)
于 2012-06-21T20:52:20.030 に答える