Php preg_match_all after散列标记在字符串中的下一个散列标记之前
我想找到(如果它们存在)任何散列标记,并获得每个标记的第一个共现项加上它后面的文本,如果存在下一个标记,则在下一个标记之前 *并非所有消息字符串都带有哈希标记 以下是我试图做的: 发送到脚本的可能字符串示例列表: 1) $message='添加了一些新内容' 2) $message='#平衡X的平衡运动' 3) $message='#更改一些日志文本#修复一些其他日志文本' $num=prereg_match_all('@?????@',$message,$matches) 这是我希望从匹配中获得的数组结构结果: 示例1的结果) 示例2的结果) 示例3的结果) 让我抓狂的事情试图在REGEX上找到合适的文档试试下面的代码:Php preg_match_all after散列标记在字符串中的下一个散列标记之前,php,regex,preg-match-all,hashtag,Php,Regex,Preg Match All,Hashtag,我想找到(如果它们存在)任何散列标记,并获得每个标记的第一个共现项加上它后面的文本,如果存在下一个标记,则在下一个标记之前 *并非所有消息字符串都带有哈希标记 以下是我试图做的: 发送到脚本的可能字符串示例列表: 1) $message='添加了一些新内容' 2) $message='#平衡X的平衡运动' 3) $message='#更改一些日志文本#修复一些其他日志文本' $num=prereg_match_all('@?????@',$message,$matches) 这是我希望从匹配中获
$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
)
)
*/
对于所有测试用例,请尝试以下方法:
$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";
}
}
}
我刚拿到!经过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)
)
)
试图在REGEX上找到合适的文档-应该可以在网上找到*有关一些有用的工具或更好的教程,请参见和。对于不包含hashtag.Nope的测试用例,这将失败。使用OP的所有输入字符串进行测试,所有返回都如预期的那样。我的坏消息-我注意到您的正则表达式专门寻找哈希标记,没有它就无法匹配。另外,我没有运行你在
preg\u replace
之后所做的额外处理来查看发生了什么。我解决了它,bsdnoobz是最接近的,但这个愚蠢的网站在接下来的6小时内不会让我发布我自己问题的答案。。。。。。
$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
$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)
)
)