Php 删除最后一个句号“后的文本”&引用;

Php 删除最后一个句号“后的文本”&引用;,php,Php,我想删除给定段落中最后出现“.”之后的所有文本 例如,从这一段中,我想删除最后一个句号之后的所有文本 与流行的观点相反,Lorem Ipsum不是简单的随机文本。它起源于公元前45年的一段古典拉丁文学,距今已有2000多年的历史。丰富的 段落的长度是动态的,唯一要删除的是最后一段之后的文本。“您可以使用和: 也许是这样的: new_string = substr(old_string, 0, strchr(old_string, '.')); strchr在旧字符串中查找上次出现的“.”的位置

我想删除给定段落中最后出现“.”之后的所有文本

例如,从这一段中,我想删除最后一个句号之后的所有文本

与流行的观点相反,Lorem Ipsum不是简单的随机文本。它起源于公元前45年的一段古典拉丁文学,距今已有2000多年的历史。丰富的

段落的长度是动态的,唯一要删除的是最后一段之后的文本。“

您可以使用和:


也许是这样的:

new_string = substr(old_string, 0, strchr(old_string, '.'));
strchr
旧字符串中查找上次出现的“.”的位置
substr
返回一个新字符串,该字符串由位置0和
strchr
找到的位置之间的
old_string
中的字符组成


    • 您可以使用explode来完成

      $summary=分解(“.”,$summary)

      $summary=$summary[1]


      在这里,您将得到文本,直到我的第一个句号,上面的解决方案不起作用,所以最后我自己得到了一些代码,这样可能会对其他人有所帮助

      <?php 
          $trim_content = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Rich";
          $place = strripos($trim_content,".");
          $content = substr($trim_content,$place);
          echo str_replace($content,".",$trim_content);
      ?>
      
      
      
      谢谢,这很好用。。。有没有办法删除文本并保留最后一个句号?@harjeethingh如果它解决了你的问题,为什么不接受这个答案呢?:)<代码>$without点
包含最后一个句号的段落。你说的删除文本是什么意思?将解析文本附加到
$段落
?要保留句号,只需将第二个参数更改为:
strhr(旧字符串“.”)+1
这不对
strchr
查找第一个事件,
strrchr
查找最后一个事件
<?php 
    $trim_content = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Rich";
    $place = strripos($trim_content,".");
    $content = substr($trim_content,$place);
    echo str_replace($content,".",$trim_content);
?>