12

次のような説明がある場合:

「話し合うだけでなく、答えられる質問を好みます。詳細を提供します。明確かつ簡単に書いてください。」

そして私が欲しいのは:

「私たちは、話し合うだけでなく、答えることができる質問を好みます。」

「[。!\?]」のような正規表現を検索し、strposを決定してから、メインの文字列からsubstrを実行すると思いますが、これは一般的なことだと思います。その周り。

4

7 に答える 7

23

ただし、文のターミネータとして複数のタイプの句読点を選択する場合は、少しコストのかかる表現の方が適しています。

$sentence = preg_replace('/([^?!.]*.).*/', '\\1', $string);

終了文字の後にスペースが続くものを検索します

$sentence = preg_replace('/(.*?[?!.](?=\s|$)).*/', '\\1', $string);
于 2009-07-16T05:09:50.087 に答える
8
<?php
$text = "We prefer questions that can be answered, not just discussed. Provide details. Write clearly and simply.";
$array = explode('.',$text);
$text = $array[0];
?>
于 2009-07-16T05:08:13.723 に答える
4

以前の正規表現はテスターでは機能しているように見えましたが、実際のP​​HPでは機能していませんでした。私はこの回答を編集して、完全で機能するPHPコードと、改良された正規表現を提供しました。

$string = 'A simple test!';
var_dump(get_first_sentence($string));

$string = 'A simple test without a character to end the sentence';
var_dump(get_first_sentence($string));

$string = '... But what about me?';
var_dump(get_first_sentence($string));

$string = 'We at StackOverflow.com prefer prices below US$ 7.50. Really, we do.';
var_dump(get_first_sentence($string));

$string = 'This will probably break after this pause .... or won\'t it?';
var_dump(get_first_sentence($string));

function get_first_sentence($string) {
    $array = preg_split('/(^.*\w+.*[\.\?!][\s])/', $string, -1, PREG_SPLIT_DELIM_CAPTURE);
    // You might want to count() but I chose not to, just add   
    return trim($array[0] . $array[1]);
}
于 2009-07-16T14:08:14.330 に答える
3

これを試して:

$content = "My name is Younas. I live on the pakistan. My email is **fromyounas@gmail.com** and skype name is "**fromyounas**". I loved to work in **IOS development** and website development . ";

$dot = ".";

//find first dot position     

$position = stripos ($content, $dot); 

//if there's a dot in our soruce text do

if($position) { 

    //prepare offset

    $offset = $position + 1; 

    //find second dot using offset

    $position2 = stripos ($content, $dot, $offset); 

    $result = substr($content, 0, $position2);

   //add a dot

   echo $result . '.'; 

}

出力は次のとおりです。

私の名前はYounasです。私はパキスタンに住んでいます。

于 2013-03-29T20:49:34.530 に答える
0

これを試して:

reset(explode('.', $s, 2));
于 2009-07-16T05:09:44.470 に答える
0
current(explode(".",$input));
于 2009-07-16T05:11:24.107 に答える
0

私はおそらくPHPで多数のsubstring/string-split関数のいずれかを使用します(いくつかはすでにここで言及されています)。ただし、「。」だけでなく、「。」または「。\ n」(場合によっては「。\ n \ r」)も探してください。何らかの理由で、文の後にスペースが続かないピリオドが含まれている場合に備えて。それはあなたが本物の結果を得る可能性を固めるだろうと思います。

たとえば、「。」だけを検索します。の上:

"I like stackoverflow.com."

あなたを取得します:

"I like stackoverflow."

本当に、私はあなたが好むと確信しています:

"I like stackoverflow.com."

そして、その基本的な検索を行うと、おそらく何かを見逃す可能性のある1つか2つの機会に出くわすでしょう。あなたがそれで走るように調整してください!

于 2009-07-16T05:19:03.483 に答える