在第一个“。”之前得到一个字符串。用PHP

时间:2011-04-07 18:43:06

标签: php

“Lorem Ipsum只是印刷和排版行业的虚拟文本。”

“Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是业界标准的虚拟文本,当时一个未知的打印机采用了类型的厨房,并将其拼凑成一个类型的样本书。它不仅存在了五个世纪,而且还延续了电子排版,基本保持不变。它在20世纪60年代推出了包含Lorem Ipsum段落的Letraset表格,最近还发布了包括Aldus PageMaker在内的桌面出版软件。 Lorem Ipsum。“

我们怎么能这样做?在这样做的事情上,php有很好的功能吗?

由于

Adam Ramadhan

5 个答案:

答案 0 :(得分:5)

对于PHP 5.3及更高版本,您可以将front_needle参数与strstr:

一起使用
strstr( $youstringhere, ".", true );

答案 1 :(得分:4)

您可以使用explode()功能将其拆分。

$sentences = explode (".", $text);
// first sentence is in $sentences[0]

答案 2 :(得分:4)

这样的事情怎么样(其他人建议使用explode - 所以我建议另一个解决方案)

$str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

if (preg_match('/^([^\.]*)/', $str, $matches)) {
    echo $matches[1] . '.';
}


正则表达式将:

  • 从字符串的开头开始:^
  • 匹配任何不是的东西。 :[^\.]
  • 任意次:[^\.]*

并且,如果你想在输出的末尾有一个..与正则表达式不匹配,那么在使用正则表达式找到的内容时,你必须将其添加回来

答案 3 :(得分:4)

// fastest way
echo substr($text, 0, strpos('.', $text));

答案 4 :(得分:3)

您可以使用explode()来获取第一句话。 http://de.php.net/manual/en/function.explode.php