1

PHP で、次の文を切り取りたいと思います。

「テスト 1。テスト 2。テスト 3。」

これを 2 つの文字列に変換します。

「テスト 1。テスト 2。」および「テスト 3」。

どうすればこれを達成できますか?

strpos を使用しますか?

ご指摘ありがとうございます。

4

5 に答える 5

1
function isIndex($i){
    $i = (isset($i)) ? $i : false;
    return $i;
}
$str = explode("2.", "Test 1. Test 2. Test 3.");
$nstr1 = isIndex(&$str[0]).'2.';
$nstr2 = isIndex(&$str[1]);
于 2011-10-17T19:41:30.987 に答える
1

最初の 2 つの文を分離するには、これですばやく実行できます。

$str = "Lorem Ipsum dolor sit amet etc etc. Blabla 2. Blabla 3. Test 4.";
$p1 = "";
$p2 = "";
explode_paragraph($str, $p1, $p2); // fills $p1 and $p2
echo $p1; // two first sentences
echo $p2; // the rest of the paragraph


function explode_paragraph($str, &$part1, &$part2) {
    $s = $str;
    $first = strpos($s,"."); // tries to find the first dot
    if ($first>-1) {
        $s = substr($s, $first); // crop the paragraph after the first dot
        $second = strpos($s,"."); // tries to find the second dot
        if ($second>-1) { // a second one ?
            $part1 = substr($str, 9, $second); // 
            $part2 = substr($str, $second);
        } else { // only one dot : part1 will be everything, no part2
            $part1 = $str;
            $part2 = "";
        }
    } else { // no sentences at all.. put something in part1 ?
        $part1 = ""; // $part1 = $str;
        $part2 = "";
    }
}
于 2011-10-18T00:26:33.990 に答える
0

そんな感じ?

$str = 'Test 1. Test 2. Test 3.';
$strArray = explode('.', $str);
$str1 = $strArray[0] . '. ' . $strArray[1] . '.';
$str2 = $strArray[2] . '.';
于 2011-10-17T19:25:30.913 に答える