Php 正则表达式获取句子中的文本
下面是我在语言配置中的一条消息。我想检测可能被随机选择的单词以显示给用户 什么是正则表达式,以检测随机词的可能性为我加粗 [好极了|祝贺|干得好|胡雷|耶哈|哇]!你现在要成为冠军了。继续走 Php开发要选择一个单词来显示,我不确定您是否需要正则表达式。我会这样做:Php 正则表达式获取句子中的文本,php,regex,Php,Regex,下面是我在语言配置中的一条消息。我想检测可能被随机选择的单词以显示给用户 什么是正则表达式,以检测随机词的可能性为我加粗 [好极了|祝贺|干得好|胡雷|耶哈|哇]!你现在要成为冠军了。继续走 Php开发要选择一个单词来显示,我不确定您是否需要正则表达式。我会这样做: function getMessage () { $words = ["Bravo","Great","Congratulations","Well done","Hoorey","Yeehaa","Wow"]; r
function getMessage () {
$words = ["Bravo","Great","Congratulations","Well done","Hoorey","Yeehaa","Wow"];
return $words [rand (0, count ($words) - 1] . "! You are now to top to be a winner. Keep going!";
}
代码
结果 输入 输出 解释
在行首断言位置^
匹配除[^!]+
之外的任何字符代码>一次或多次
积极前瞻,确保后续内容匹配(?=!)
代码>字面意思
/(好极了,恭喜你,干得好,Hoorey,Yeehaa,哇)!您现在将成为冠军\。继续走/代码>为什么不直接使用?谢谢。但这是最后一个选择,因为内容提供商提供的文本太多,需要管理。
^[^!]+(?=!)
Bravo! You are now to top to be a winner. Keep going!
Great! You are now to top to be a winner. Keep going!
Congratulations! You are now to top to be a winner. Keep going!
Well done! You are now to top to be a winner. Keep going!
Hooray! You are now to top to be a winner. Keep going!
Yeehaa! You are now to top to be a winner. Keep going!
Wow! You are now to top to be a winner. Keep going!
Bravo
Great
Congratulations
Well done
Hoorey
Yeehaa
Wow