Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中从变量返回第一句话_Php_Regex - Fatal编程技术网

在PHP中从变量返回第一句话

在PHP中从变量返回第一句话,php,regex,Php,Regex,我已经找到了一条类似的线索: $sentence = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $string); 不过,这在我的功能中似乎不起作用: <?function first_sentence($content) { $content = html_entity_decode(strip_tags($content)); $content = preg_replace('/(.*?[?!.](?=\s|$)

我已经找到了一条类似的线索:

$sentence = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $string);
不过,这在我的功能中似乎不起作用:

<?function first_sentence($content) {
    $content = html_entity_decode(strip_tags($content));   
    $content = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $content);
    return $content;


}?>


当一个句子作为一段的结尾时,似乎没有考虑第一句话。有什么想法吗?

这里是正确的语法,很抱歉我的第一个回答:

function firstSentence($string) {
      $sentences = explode(".", $string);
      return $sentences[0];
}

如果你需要n个句子,你可以使用下面的代码。 我将代码改编自:&

/**
*获取一个字符串的前n个句子。
*
*如果未找到结尾标点符号,则$text将被删除
*作为判决返回。如果设置了$strict
*如果为TRUE,则将返回FALSE。
*
*@param string$text文本
*int$no_句子要提取的句子数
*@param boolean$strict语句*必须*以$end字符之一结尾
*@param string$结束标点符号
*@return string | bool语句,如果未找到,则为FALSE
*/
函数first句子($text,$no_句,$strict=false,$end='。?!;:'){

$result=preg_split('/(?你能提供一个到原始线程的链接吗?请不要介意,我想我已经找到了它。)你有没有看过其他的“带正则表达式的第一句话”问题,比如这个问题,这个看起来很有希望/^。{150,}?[!]+(?=\s |$)/但我不确定如何将其合并到函数中当一个段落结束时,你的意思是什么?在这种情况下是否还有句点,或者它使用了不同的delimeter?“\\1”的函数是什么?我也尝试使用它,但是当有多个段落出现时,我会返回多个句子。这应该是ld绝对不是公认的解决方案。斯维里·奥尔森说得对。我敢打赌70.4%以上的句子都不是以点结尾的!!!我说得对吗?我想我是的。十年后,我完全同意其他人的看法。我的解决方案很糟糕。我很想了解关于空$result的返回严格错误的东西。
/**
 * Get the first sentence of a string.
 *
 * If no ending punctuation is found then $text will
 * be returned as the sentence. If $strict is set
 * to TRUE then FALSE will be returned instead.
 *
 * @param  string  $text   Text
 * @param  boolean $strict Sentences *must* end with one of the $end characters
 * @param  string  $end    Ending punctuation
 * @return string|bool     Sentence or FALSE if none was found
 */
function firstSentence($text, $strict = false, $end = '.?!') {
    preg_match("/^[^{$end}]+[{$end}]/", $text, $result);
    if (empty($result)) {
        return ($strict ? false : $text);
    }
    return $result[0];
}

// Returns "This is a sentence."
$one = firstSentence('This is a sentence. And another sentence.');

// Returns "This is a sentence"
$two = firstSentence('This is a sentence');

// Returns FALSE
$three = firstSentence('This is a sentence', true);
/**
 * Get n number of first sentences of a string.
 *
 * If no ending punctuation is found then $text will
 * be returned as the sentence. If $strict is set
 * to TRUE then FALSE will be returned instead.
 *
 * @param  string  $text   Text
 * int     $no_sentences   Number of sentences to extract
 * @param  boolean $strict Sentences *must* end with one of the $end characters
 * @param  string  $end    Ending punctuation
 * @return string|bool     Sentences or FALSE if none was found
 */

function firstSentences($text, $no_sentences, $strict = false , $end = '.?!;:') {

    $result = preg_split('/(?<=['.$end.'])\s+/', $text, -1, PREG_SPLIT_NO_EMPTY);

    if (empty($result)) {
        return ($strict ? false : $text);
    }

    $ouput = '';
    for ($i = 0; $i < $no_sentences; $i++) {
        $ouput .= $result[$i].' ';
    }
    return $ouput;

}