Php 获取包含特定单词的所有句子
我试图从包含一组句子的文本中获取所有句子: 这是我的密码和密码Php 获取包含特定单词的所有句子,php,regex,Php,Regex,我试图从包含一组句子的文本中获取所有句子: 这是我的密码和密码 这将解决您的问题 <?php $var = array('one','of','here','Another'); $str = 'Start of sentence one. This is a wordmatch one two three four! Another, sentence here.'; foreach ($var as $val) { if (stripos($str,$val)
这将解决您的问题
<?php
$var = array('one','of','here','Another');
$str = 'Start of sentence one. This is a wordmatch one two three four! Another, sentence here.';
foreach ($var as $val)
{
if (stripos($str,$val) !== false)
{
echo $val;
echo "\n";
}
}
我想说你的方法有点太复杂了。更容易:
先把所有的句子都记下来
然后根据您的条件筛选此集合
例如:
演示:
注意事项关于:
if (preg_match_all('/.+?[!.]\s*/', $text, $match)) {
关于模式:
.+? // select at least one char, ungreedy
[!.] // until one of the given sentence
// delimiters is found (could/should be extended as needed)
\s* // add all following whitespace
只是做它的名字所暗示的。它返回输入数组的过滤版本(此处$match[0]
)。为数组的每个元素调用提供的回调函数(内联函数)get,并应返回true/false,以确定当前元素是否应为新数组的一部分。
允许访问函数内部所需的$pinees
-数组。对于1。
请改用,因为只匹配第一次出现的情况…@Enissay:它给出了PHP通知:第11行的/home/QuV0Jp/prog.PHP中的数组到字符串转换
,所以您希望得到一个句子列表,其中至少包含$var
中的一个关键字?@Programming\u疯狂,在这种情况下,preg\u match\u all将返回数组数组数组,所以使用print\r($match[0])代码>显示它@Enissay:我怎样才能将所有这些句子以不同的方式排列成一个数组?@Sohaii:谢谢,但我不明白,你能在这里举个例子吗:Ideone.com?它不能解决任何问题,你的答案是否缺少代码?我编辑了代码,请再次检查,stripos-查找字符串中第一个不区分大小写的子字符串的位置--int stripos(字符串$haystack,字符串$Pineder[,int$offset=0])我检查了它,但它给出了相同的结果,你能把它放在Ideone上吗?我已经更新了代码,请再次检查这将解决你的重复问题谢谢老兄,你能给我解释一下吗?
$hits=array\u filter($match[0],function($句子)使用($pines)
再多解释一下too@Programming_crazy我加了一些额外的注释,希望能有所帮助。
if (preg_match_all('/.+?[!.]\s*/', $text, $match)) {
.+? // select at least one char, ungreedy
[!.] // until one of the given sentence
// delimiters is found (could/should be extended as needed)
\s* // add all following whitespace
array_filter($match[0], function ($sentence) use($needles) {