For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > September 2006 > preg_replace() Help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author preg_replace() Help
ralphNOSPAM@primemail.com

2006-09-23, 6:57 pm

I've seen several 'word wrapping' answers here using preg_replace(),
however, none of them address not splitting the last word if it falls
on the wordwraplen value. Is there a way to use preg_replace() and
have the wrap occur at the beginning of the last word so it is not cut
off?

I'm looking to word wrap text at a variable length and inserting a
'<br>' at the end of each line so I can see correctly in my browser.

So if I have this text:

"Now is the time for all good men to come to the aid of their party"
and I want to word wrap at 27 I don't want this:

"Now is the time for all go"
"od men to come to the aid of their party"

I want this:

"Now is the time for all"
"good men to come to the aid of their party"

Janwillem Borleffs

2006-09-24, 3:57 am

ralphNOSPAM@primemail.com wrote:
> I want this:
>
> "Now is the time for all"
> "good men to come to the aid of their party"
>


If you would settle for the following, you can simply use wordwrap($text,
27, '<br>'):

Now is the time for all
good men to come to the aid
of their party


JW


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com