Helpful Information
 
 
Category: Post a PHP snippet
FUNCTION: TrimByWords()

I wrote this because I was bored and had nothing to do. I also wanted to take the long way about it (at least I think its long?) to consume time. Thought I'd share it with you bre. :)



<?php

$str = 'In the dawn of time man learned to live off of nature around him. Eating nuts, roots, plants, meat and other assorted foods.';

function TrimByWords($str, $count) {

$trim = explode(" ", str_replace(" ", " ", $str));
$message = "";
for ($i = 0; $i < $count; $i++) {
if ($i == $count) {
$space="";
} else {
$space=" ";
}
if (!(strpos($trim[$i], "."))) {
$message .= $trim[$i] . $space;
} else {
$message .= $trim[$i];
$i = $count;
}
}
$messag .= " ....";
return $message;

}

echo TrimByWords($str, 15); // This is set to stop one word after the period, but since there is a period it stops short just to create a trim that makes better sense, and is a different length then other trims.

?>


So that example would output:


In the dawn of time man learned to live off of nature around him. ....

Where as if it ignored the periods it would output:


In the dawn of time man learned to live off of nature around him. Eating ....

I like to have a go at things:


<?php
function wordswrap($text, $words)
{
$text = preg_split('/\s+/', $text); // No weird spaces, tabs & newlines.
if(count($text) <= $words)
{
return $text;
}
$ret = implode(' ', array_slice($text, 0, $words));
if(in_array($ret{strlen($ret) - 1}, array('.', '!', '?', ':', ';', ','))) // Not only .
{
$ret .= ' ' . $text[$words];
}
return $ret . '...';
}

$text = 'Hello, I\'m new! What\'s going on?';

print wordswrap($text, 6);
?>

I like to have a go at things:


<?php
function wordswrap($text, $words)
{
$text = preg_split('/\s+/', $text); // No weird spaces, tabs & newlines.
if(count($text) <= $words)
{
return $text;
}
$ret = implode(' ', array_slice($text, 0, $words));
if(in_array($ret{strlen($ret) - 1}, array('.', '!', '?', ':', ';', ','))) // Not only .
{
$ret .= ' ' . $text[$words];
}
return $ret . '...';
}

$text = 'Hello, I\'m new! What\'s going on?';

print wordswrap($text, 6);
?>


Nice, I like your character safe-guard.

I caught a few problems with mine I think.

1) What if they set the trim too 20 and the period is in the 6th word?
2) What is spaces were alt spaces, or any other method of a space?
3) What if words didn't contain spaces and its nessessary to trim that word?

1) Nothing. If the last character isn't one of those chars nothing else will happen.
2) You don't really need them in the preview.
3) You break the string at spaces too. It would work the same way with the exception of breaking at newlines and tabs aswell.

1) Nothing. If the last character isn't one of those chars nothing else will happen.
2) You don't really need them in the preview.
3) You break the string at spaces too. It would work the same way with the exception of breaking at newlines and tabs aswell.

I think with question number one it would stop at the 6th character. Because its looking for a period to stop at as well as finding the trim limit.

Yours might be best to use I think. (For anyone reading.) Unless your strings to trim would work with mine.

It doesn't "look" for anything. There are no loops in my code.

No no, I said I found problems in my method. :thumbsup:

I didn't see that coming. I didn't think you'd be asking yourself questions.

I didn't see that coming. I didn't think you'd be asking yourself questions.


Well now you know... I talk to myself! :eek: But the questions were mainly so other people the plan on looking at the code know. :D

Guys, you both missed something there.

... !== …

The first one is three periods. The second one is a single character, an ellipsis; the typographical symbol intended to represent the ommiting of a section (or the remainder) of a piece of text.

Feel free to replace your dots with & #8230; [minus the space] if that's what you really meant ;)

Guys, you both missed something there.

... !== …

The first one is three periods. The second one is a single character, an ellipsis; the typographical symbol intended to represent the ommiting of a section (or the remainder) of a piece of text.

Feel free to replace your dots with & #8230; [minus the space] if that's what you really meant ;)

... Don't even know what you mean. Both ways work fine.

... Don't even know what you mean. Both ways work fine.
... is not the same as …

The first is three full stops; it has no special meaning.

The second is an ellipsis; it indicates something has been omitted.

Using 3 full stops is grammatically incorrect; however, using an ellipsis is correct.

It is just a new char acteradded for an unknown reason (for me).
"The something being omitted" is (...) in my language.
It is still grammatically correct as an ellipsis is three dots.

It is just a new char acteradded for an unknown reason (for me).
"The something being omitted" is (...) in my language.
It is still grammatically correct as an ellipsis is three dots.

H is correct, and infact its four dots to show an omitted string parse, three dots is actually a delay where as four is a delayed stop

For example.

"I don't like eggs... they give me gas."
Or:
"I don't like eggs, they give me gas...."

The HTML entity for the three dots is just a simple way to do it, alowing less space between dots to show it as a single character.

Obviously typing has nothing to do with grammar, three dots by hand or HTML, same concept. When writing there is no special way of doing dots, no its just three dots.

When writing there is no special way of doing dots, no its just three dots.
On OS X you can quite simply do Alt + ; to get an ellipsis…

On OS X you can quite simply do Alt + ; to get an ellipsis…


Writing, not typing.

H is correct, and infact its four dots to show an omitted string parse, three dots is actually a delay where as four is a delayed stop

For example.

"I don't like eggs... they give me gas."
Or:
"I don't like eggs, they give me gas...."

The HTML entity for the three dots is just a simple way to do it, alowing less space between dots to show it as a single character.

Obviously typing has nothing to do with grammar, three dots by hand or HTML, same concept. When writing there is no special way of doing dots, no its just three dots.

I was happy to walk away from this thread having at least attempted to include correct typographical practices.

But you are spreading semi-incorrect information as though it is fact; that doesn't sit well with me considering the intended permanency of these posted snippet threads.

Here's the facts: http://en.wikipedia.org/wiki/Ellipsis

P.S -Element, It's never 4 dots. It's 1 period, one full space and one horizontal ellipsis.
Don't say in your first reply you don't knnow what I mean and then in your second reply try to define correct usage. You'll only misguide less experienced members.

I was happy to walk away from this thread having at least attempted to include correct typographical practices.

But you are spreading totally incorrect information; that doesn't sit well with me considering the intended permanency of this posted snippet threads.

Here's the facts: http://en.wikipedia.org/wiki/Ellipsis

P.S -Element, It's never 4 dots. It's 1 period, one full space and one horizontal ellipsis.
Don't say in your first reply you don't knnow what I mean and then in your second reply try to define correct usage. You'll only misguide less experienced members.

1) Its not incorrect, its from a book.
2) I don't need wikipedia to tell me what a grammar book can
3) Yes, four dots. Like in games when it says "Loading...." Its like saying "Loading.", except your making a delay between anything else said after that.

Like:

Green eggs and ham.... Same I am.

Instead of a pause like a comma, its a delay between the next sentance.

4) No, the first post I thought you were saying you can't use dots inside the string, and were saying to replace it with the HTML entity, that has nothing to do with the meaning of ellipsis.

1) Its not incorrect, its from a book.
Book are always right, obviously. My HTML 2 book tells me that that certain methods are the "best and most up to date", but this isn't true anymore.


2) I don't need wikipedia to tell me what a grammar book can
Wikipedia is great, and usually much much more accurate than any book.


3) Yes, four dots. Like in games when it says "Loading...." Its like saying "Loading.", except your making a delay between anything else said after that.

Have you ever considered that the game designer didnt take the correct english involved. In fact, most games I see use 3 dots.



Like:

Green eggs and ham.... Same I am.

Instead of a pause like a comma, its a delay between the next sentance.

I have never seen this used apart from here.










privacy (GDPR)