By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

Break Word

P: n/a
Hi there,

I have a form, and i want to prevent
words (inserted in a multiline textfield)
to become too long. (So my lay-out won't be
messed up)

How can i detect after submitting if any word
from the field is longer then e.g. 15 chars.
If so, break it at the 14th char, add a dash,
an continue at the next line...

For example:
Thiswordislongerthenfifteencharacters

becomes

Thiswordislong-
erthenfifteenc-
haracters

Any help is greatly appreciated!

Greetings knoak
Jul 17 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
In article <a5*************************@posting.google.com> , knoak wrote:
How can i detect after submitting if any word
from the field is longer then e.g. 15 chars.
If so, break it at the 14th char, add a dash,
an continue at the next line...


It depends on what you depend what a word is.
Assuming that every group of characters that is separated by space is
defined as a word.

You could easily lookup with strpos where the spaces occur in a string.
The distance between 2 such positions would be the length of the word.

Or you could use the split function to get all the "words" and use
strlen to determine how long they are.

After this you can use substr to grab parts out of such a word.

More on the functions mentionned above can be found at
http://www.php.net/manual -> string functions.

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Jul 17 '05 #2

P: n/a
I'm sorry,

but since i'm quite new tot PHP i
really don't know where to start.

Can someone help me out a bit further?
Thanks anyway.

Greetings knoakske
Jul 17 '05 #3

P: n/a
if(strlen($word) > 25){
$word = substr($word, 0, 25) . ' ' .substr($word, 25, strlen($word));
}
Jul 17 '05 #4

P: n/a
kn******@hotmail.com (knoak) wrote in message news:<a5*************************@posting.google.c om>...
Hi there,

I have a form, and i want to prevent
words (inserted in a multiline textfield)
to become too long. (So my lay-out won't be
messed up)

How can i detect after submitting if any word
from the field is longer then e.g. 15 chars.
If so, break it at the 14th char, add a dash,
an continue at the next line...


http://in.php.net/wordwrap

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Jul 17 '05 #5

P: n/a
I used the upper example,
but now it breaks everything:

thisisoneverylongwor-
danditgetsbroken and-
these are seperate-
words and get broken-
as well
instead of

thisisoneverylongwor-
danditgetsbroken and
these are seperate
words and get broken
as well

so if there's a space, there's no need for a
break with a line.

thanks in advance.

Greetings knoakske
Jul 17 '05 #6

P: n/a
I'm not sure exactly what you want, but I think you want to take a
sentence and check to see if there are any words bigger then a certain
limit. The sentence itself is a string so all the examples break your
sentence up regardless of whether or not there is a really long word.
Is that right?

If it is you just need to use the explode function to break your
sentence up into an array of words. Then cycle through it with a
foreach to find any really long words and use one of the above
examples to break that word when you find it.
Jul 17 '05 #7

P: n/a
knoak wrote:
How can i detect after submitting if any word
from the field is longer then e.g. 15 chars.
If so, break it at the 14th char, add a dash,
an continue at the next line...


You have to do this with an regular expression. See preg_replace in the
php manual.

greetings Christian
Jul 17 '05 #8

P: n/a
kn******@hotmail.com (knoak) wrote in message news:<a5*************************@posting.google.c om>...
Hi there,

I have a form, and i want to prevent
words (inserted in a multiline textfield)
to become too long. (So my lay-out won't be
messed up)


I've been struggling with the same thing. Near as I can figure, the
best way is to use a regular expression, like:

[ \S]{20}

I myself was unsure how to replace the words with broken versions of
the same word. I got some help with long urls here:

http://groups.google.com/groups?hl=e....uni-berlin.de
Jul 17 '05 #9

P: n/a
lawrence wrote:
I've been struggling with the same thing. Near as I can figure, the
best way is to use a regular expression, like:

[ \S]{20}

I myself was unsure how to replace the words with broken versions of
the same word.


$target = preg_replace('(\S{20})(?=\S)','\1 ',$target);

enjoy :)
Jul 17 '05 #10

P: n/a
kn******@hotmail.com (knoak) wrote in message news:<a5**************************@posting.google. com>...
I used the upper example,
but now it breaks everything:

thisisoneverylongwor-
danditgetsbroken and-
these are seperate-
words and get broken-
as well
instead of

thisisoneverylongwor-
danditgetsbroken and
these are seperate
words and get broken
as well

so if there's a space, there's no need for a
break with a line.


Usernotes are great resources. You seem to have missed them. Not
sure, if this is what you wanted:

<?php
$text = <<<EOT
thisisoneverylongwordanditgetsbroken and these are seperate words and
get broken as well
EOT;
//Next line logic from http://in.php.net/wordwrap#31342
$text = preg_replace ("/([^\s]{15,})/e", "''.wordwrap('\\1', 14,
'-\n', 1).''", $text);
$text = wordwrap($text, 15, "\n");
echo $text;
?>

BTW, please properly snip and quote other messages.

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Jul 17 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.