问题描述:

"Lorem Ipsum is simply dummy text of the printing and typesetting industry."

from

"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."

how can we do that ? is there a good function in php for doing things like this ?

Thanks

Adam Ramadhan

网友答案:
// fastest way
echo substr($text, 0, strpos('.', $text));
网友答案:

For PHP 5.3 and later you could use the before_needle argument with strstr:

strstr( $youstringhere, ".", true );
网友答案:

You can split it using the explode() function.

$sentences = explode (".", $text);
// first sentence is in $sentences[0]
网友答案:

What about something like this (others have suggested using explode -- so I'm suggesting another solution) :

$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] . '.';
}


The regex will :

  • Start at beginning of string : ^
  • Match anything that's not a . : [^\.]
  • Any number of times : [^\.]*

And, as you wanted a . at the end of the output and that . is not matched by the regex, you'll have to add it back when using what's been found by the regex.

网友答案:

You could use explode() to get the first sentence. http://de.php.net/manual/en/function.explode.php

相关阅读:
Top