在第二个句号后裁剪字符串

发布于 2024-12-10 13:16:49 字数 207 浏览 0 评论 0原文

在 PHP 中,我想裁剪以下句子:

“测试 1。测试 2。测试 3。”

并将其转换为 2 个字符串:

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

我该如何实现这一目标?

我使用 strpos 吗?

非常感谢您的指点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故事↓在人 2024-12-17 13:16:50
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]);
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]);
離人涙 2024-12-17 13:16:50

要分开前两个句子,应该可以快速完成:

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

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 = "";
    }
}
诠释孤独 2024-12-17 13:16:50
$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;
    }
$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;
    }
彡翼 2024-12-17 13:16:50

类似的事情?

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

Something like that?

$str = 'Test 1. Test 2. Test 3.';
$strArray = explode('.', $str);
$str1 = $strArray[0] . '. ' . $strArray[1] . '.';
$str2 = $strArray[2] . '.';
戒ㄋ 2024-12-17 13:16:50

您必须在“测试 2”之后剪切文本的主要原因是什么?而不是之前?最好的解决方案取决于您想要做什么以及您最终想要做什么

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文