2
这里是我的总结awesome字符串函数:如何使用preg_match_all在干草堆中找到复数针?
function summarize($haystack, $needle, $wordLimit=1) {
$preg_safe = str_replace(" ", "\s", preg_quote($needle));
$pattern = "/(\w*\S\s+){0,$wordLimit}\S*\b($preg_safe)\b\S*(\s\S+){0,$wordLimit}/ix";
if (preg_match_all($pattern, $haystack, $matches)) {
return $matches[0][0];
}
return false;
}
该功能会在大串针,并用针周围背景的ň话一起返回。有点像谷歌搜索结果显示搜索字符串在标题下的结果的上下文中。
下面是我如何使用它:
$haystack = 'Lorem ipsums dolor sit amet.'; // note the plural "ipsum"
echo summarize($haystack,'ipsums');
返回:Lorem ipsums dolor
(默认为1个字针周围)
echo summarize($haystack,'ipsum'); // i.e. not exact word match of "ipsums"
返回:false
我怎样才能调整正则表达式返回相同的结果确切的单词匹配?
什么是“牛”的复数?是“牛”吗?线索:不是。你打算如何处理? – 2012-03-23 19:21:42
您可以通过复合功能始终运行输入针。 Nethack有一个......有趣的一个:http://celabs.com/nethack/html/objnam_8c.html#ac1ac6c9f4a8707e6d675c75cf9bd4908 =) – Crontab 2012-03-23 19:25:55
难道你不想只拿出'\ b',所以你不要'在搜索字符串旁边需要单词边界吗? – 2012-03-23 19:27:35