Обрезать строку после 2-й полной остановки - PullRequest
1 голос
/ 17 октября 2011

В PHP я хотел бы обрезать следующее предложение:

"Испытание 1. Испытание 2. Испытание 3".

и преобразовать это в 2 строки:

«Испытание 1. Испытание 2». и «Испытание 3».

Как мне этого добиться?

Использую ли я strpos?

Большое спасибо за любые указатели.

Ответы [ 5 ]

1 голос
/ 12 октября 2017
$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;
    }
1 голос
/ 18 октября 2011

, чтобы отделить два первых предложения, это должно сделать это быстро:

$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 = "";
    }
}
1 голос
/ 17 октября 2011
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]);
0 голосов
/ 17 октября 2011

какова основная причина, по которой вы должны вырезать текст после «Теста 2». а не раньше? лучшее решение зависит от того, что вы хотите сделать и что вы в конечном итоге захотите сделать с этим

0 голосов
/ 17 октября 2011

Что-то в этом роде?

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