0

我想找到(如果它们存在)任何哈希标签,并获取每个标签的第一个共现加上它之后但在下一个标签之前的文本(如果另一个标签存在)。

*并非所有消息字符串都带有哈希标签!

这是我正在尝试做的事情:

发送到脚本的可能字符串示例列表:

1) $message = '添加了一些新内容';

2) $message = ' #BALANCE X 的平衡运动';

3) $message = ' #CHANGE一些日志文本#FIX一些其他日志文本';

$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 回答