2
votes

"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

5
we are at really basic here xD - dynamic
sory, but i need to know what people say about this :), see explode and strpos this is why i love stackoverflow. - Adam Ramadhan
have you tried php.net/string ? People wont't say anything you can't find in the manual - Your Common Sense

5 Answers

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

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

strstr( $youstringhere, ".", true );
4
votes

You can split it using the explode() function.

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

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.

3
votes

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