Connecting Tech Pros Worldwide Forums | Help | Site Map

Break Word

knoak
Guest
 
Posts: n/a
#1: Jul 17 '05
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

Tim Van Wassenhove
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Break Word


In article <a5e41e0e.0406140652.4a0bb43@posting.google.com> , knoak wrote:[color=blue]
> 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...[/color]

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>
knoak
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Break Word


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
Jay Donnell
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Break Word


if(strlen($word) > 25){
$word = substr($word, 0, 25) . ' ' .substr($word, 25, strlen($word));
}
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Break Word


knoakske@hotmail.com (knoak) wrote in message news:<a5e41e0e.0406140652.4a0bb43@posting.google.c om>...[color=blue]
> 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...[/color]

http://in.php.net/wordwrap

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
knoak
Guest
 
Posts: n/a
#6: Jul 17 '05

re: Break Word


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
Jay Donnell
Guest
 
Posts: n/a
#7: Jul 17 '05

re: Break Word


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.
Christian Fersch
Guest
 
Posts: n/a
#8: Jul 17 '05

re: Break Word


knoak wrote:[color=blue]
> 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...[/color]

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

greetings Christian
lawrence
Guest
 
Posts: n/a
#9: Jul 17 '05

re: Break Word


knoakske@hotmail.com (knoak) wrote in message news:<a5e41e0e.0406140652.4a0bb43@posting.google.c om>...[color=blue]
> 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)[/color]

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
Christian Fersch
Guest
 
Posts: n/a
#10: Jul 17 '05

re: Break Word


lawrence wrote:[color=blue]
> 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.[/color]

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

enjoy :)
R. Rajesh Jeba Anbiah
Guest
 
Posts: n/a
#11: Jul 17 '05

re: Break Word


knoakske@hotmail.com (knoak) wrote in message news:<a5e41e0e.0406170957.68c4b2c9@posting.google. com>...[color=blue]
> 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.[/color]

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
Closed Thread