Crop string after 2nd full stop

2019-08-02 19:47发布

问题:

In PHP, I'd like to crop the following sentence:

"Test 1. Test 2. Test 3."

and transform this into 2 strings:

"Test 1. Test 2." and "Test 3."

How do I achieve this?

Do I use strpos?

Many thanks for any pointers.

回答1:

Something like that?

$str = 'Test 1. Test 2. Test 3.';
$strArray = explode('.', $str);
$str1 = $strArray[0] . '. ' . $strArray[1] . '.';
$str2 = $strArray[2] . '.';


回答2:

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]);


回答3:

to separate the two first sentence, this should do it quick :

$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 = "";
    }
}


回答4:

$string="Inspired by arthropod insects and spiders, BAIUST researchers have created an entirely new type of semi-soft robots capable of standing and walking using drinking straws and inflatable tubing. Inspired by arthropod insects and spiders, BAIUST researchers have created an entirely new type of semi-soft robots capable of standing and walking using drinking straws and inflatable tubing. Inspired by arthropod insects and spiders, BAIUST researchers have created an entirely new type of semi-soft robots capable of standing and walking using drinking straws and inflatable tubing.
";

call : 
echo short_description_in_complete_sentence($string,3,1000,2);
function:
public function short_description_in_complete_sentence($string,$start_point,$end_point,$sentence_number=1){
        $final_string='';
        //$div_string=array();
        $short_string=substr($string,$start_point,$end_point);
        $div_string=explode('.',$short_string);
        for($i=0;$i<$sentence_number;$i++){
            if(!Empty($div_string[$sentence_number-1])){
            $final_string=$final_string.$div_string[$i].'.';
        }else{ $final_string='Invalid sentence number or total character number!';}
        }
        return $final_string;
    }


回答5:

what is the main reason why you must cut the text after "Test 2." and not before ? the best solution depends on what you want to do and what you will eventually want to do with that