Connecting Tech Pros Worldwide Help | Site Map

Break words, but not tags

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 12:17 PM
frizzle
Guest
 
Posts: n/a
Default Break words, but not tags

Hi there,

I have data drawn from a mySQL DB. I used to massage the text like
this:
nl2br(wordwrap(htmlentities(stripslashes($entry["msg"])), 27, " ", 0))

Now i have a problem: i wanted pasted links to become real links. I
found a script to do that...
(on the bottom of this post)

I have 3 problems:
1: The script doesn't recognise image links.... (*.jpg, *.gif, etc...),
and i really don't have a clue how to fix this... It handles things
like '*.php?parameter' well.

2: if i have a very long url, it distorts my layout, so want it to be
broken, but of course not inside the tag, but only the 'visual' tag...
I tried wordwrap, but that also breaks my tags.....

3: I want the visual part not to display the 'http://' part, but of
course it has to link to that...

Hope this is not too much...

Greetz Frizzle




----------------Code------------------

function makeClickableLinks($text_string)
{
$text_string =
eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a
href="\\1" class="bodylink" target="_blank">\\1</a>', $text_string);
$text_string =
eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)',
'\\1<a href="\\2" class="bodylink" target="_blank">\\2</a>',
$text_string);
$text_string =
eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a
href="mailto:\\1" class="bodylink">\\1</a>', $text_string);
return str_replace(' target="_blank">http://',' target="_blank">',
$text_string);
};


  #2  
Old July 17th, 2005, 12:17 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: Break words, but not tags

"frizzle" <phpfrizzle@hotmail.com> wrote in message
news:1111750572.699240.156400@f14g2000cwb.googlegr oups.com...[color=blue]
> Hi there,
>
> I have data drawn from a mySQL DB. I used to massage the text like
> this:
> nl2br(wordwrap(htmlentities(stripslashes($entry["msg"])), 27, " ", 0))[/color]

Why not just let the browser deal with word-wrapping? Unless you're using a
mono-spaced font, you don't know the actual width of the text anyway.


  #3  
Old July 17th, 2005, 12:17 PM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: Break words, but not tags

frizzle wrote:[color=blue]
> 1: The script doesn't recognise image links.... (*.jpg, *.gif,
> etc...), and i really don't have a clue how to fix this... It handles
> things like '*.php?parameter' well.
>
> 2: if i have a very long url, it distorts my layout, so want it to be
> broken, but of course not inside the tag, but only the 'visual' tag...
> I tried wordwrap, but that also breaks my tags.....
>
> 3: I want the visual part not to display the 'http://' part, but of
> course it has to link to that...
>[/color]

The following might be helpful (not fully tested):

http://playground.jwscripts.com/wordwrap.phps


JW



  #4  
Old July 17th, 2005, 12:18 PM
frizzle
Guest
 
Posts: n/a
Default Re: Break words, but not tags

I don't want the browser to control it, because the text is in a frame,
and i'm trying to prevent those annoying horizontal scrollbars from
appearing.
Thanks for your suggestion anyway...

@ Jan, i'll check that out later: thanks!

Frizzle.

  #5  
Old July 17th, 2005, 12:18 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: Break words, but not tags

"frizzle" <phpfrizzle@hotmail.com> wrote in message
news:1111859102.070268.66200@f14g2000cwb.googlegro ups.com...[color=blue]
> I don't want the browser to control it, because the text is in a frame,
> and i'm trying to prevent those annoying horizontal scrollbars from
> appearing.
> Thanks for your suggestion anyway...
>
> @ Jan, i'll check that out later: thanks!
>
> Frizzle.[/color]

You just need to use the right style. Something like the following would
keep the width of an element fixed, dropping off words that are too long:

<div style="overflow: hidden; overflow: hidden; text-overflow: ellipsis; ">

There are CSS attributes for controlling word wrapping behavior too.

Word wrapping in general shouldn't be done in PHP. The result is usually
poor, since the widths of the different letters aren't taken into account.
And the text looks bad when the user crank up the text size. Since PHP
strings are byte-oriented, it also means you can't use UTF-8, where one
character can take up 2, 3 bytes.


  #6  
Old July 17th, 2005, 12:18 PM
frizzle
Guest
 
Posts: n/a
Default Re: Break words, but not tags

Hmmm, i don't understand the last part of your reply,
but i do understand the first part.

Then i wonder, are those css-properties browser proof...? (For browsers
that have it enabled of course) if yes, you've comvinced me, but please
be honest... ;)

Happy easter!

Greetings frizzle.

  #7  
Old July 17th, 2005, 12:18 PM
Andy Hassall
Guest
 
Posts: n/a
Default Re: Break words, but not tags

On Sat, 26 Mar 2005 19:10:47 -0500, "Chung Leong" <chernyshevsky@hotmail.com>
wrote:
[color=blue]
><div style="overflow: hidden; overflow: hidden; text-overflow: ellipsis; ">[/color]

Note that text-overflow is not a valid CSS attribute yet; it's part of the
CSS3 spec which isn't finished.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
  #8  
Old July 17th, 2005, 12:19 PM
frizzle
Guest
 
Posts: n/a
Default Re: Break words, but not tags

So it's not fully compatible yet, thanks for pointing that out. Then i
hope someone can help me with my original questions...

Thanks...

(i tried janwillems option, but that wasn't a big succes either, too
bad... :( it only worked for a few types of links...)

  #9  
Old July 17th, 2005, 12:19 PM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: Break words, but not tags

frizzle wrote:[color=blue]
> (i tried janwillems option, but that wasn't a big succes either, too
> bad... :( it only worked for a few types of links...)[/color]

This can be easily adjusted, just specify the links you would like to see
supported.


JW



  #10  
Old July 17th, 2005, 12:20 PM
frizzle
Guest
 
Posts: n/a
Default Re: Break words, but not tags

i'm sorry janwillem,
i really tried to make it work for me, but i'm too new i guess
for it all to make sense to me... :(

Could you maybe somehow show me an example of how i could expand this
function's functionality?

Thanks anyway..

  #11  
Old July 17th, 2005, 12:21 PM
Janwillem Borleffs
Guest
 
Posts: n/a
Default Re: Break words, but not tags

frizzle wrote:[color=blue]
> Could you maybe somehow show me an example of how i could expand this
> function's functionality?
>[/color]

The easiest way would be to change:

(http://|www)

Into:

(http://|ftp://|mailto:|www)

This way, you are extending support to include the ftp and mailto (pseudo)
protocols.

Mail me off-group if you want more assistence, because the thread is pushed
to the back by other messages and easily overlooked.


JW



  #12  
Old July 17th, 2005, 12:21 PM
frizzle
Guest
 
Posts: n/a
Default Re: Break words, but not tags

allright, very kind of you, i'll probably do that,
since i'm quite interested in making this script work right,
because it's a script that can come in handy more than once.

Thanks again janwillem...

(btw, dutch-speaking?)

  #13  
Old July 17th, 2005, 12:21 PM
Chung Leong
Guest
 
Posts: n/a
Default Re: Break words, but not tags

"frizzle" <phpfrizzle@hotmail.com> wrote in message
news:1111920454.784716.150380@f14g2000cwb.googlegr oups.com...[color=blue]
> Hmmm, i don't understand the last part of your reply,
> but i do understand the first part.[/color]

Well, it's pretty simple really. For example, it takes 2 bytes to store a
Cyrillic letter in UTF-8. PHP will see, say, 80 bytes, when in reality
there's only some 40 letters. So word wrapping would be wrong. You also run
the risk of cut in a character in half, inserting a break tag in between two
bytes on one character.

As I said, it's not a good idea to do word-wrapping in PHP. Whatever you
have might work in one particular case, but in general, you will end up
building limitations into your application. Your code will not work, for
instance, with a number of Asian languages, where space is not used to
separate word.

If possible, use CSS to control appearance of your pages.
[color=blue]
> Then i wonder, are those css-properties browser proof...? (For browsers
> that have it enabled of course) if yes, you've comvinced me, but please
> be honest... ;)[/color]

text-overflow doesn't work in Firefox and pals, but overflow does. If it end
up cutting off a long URL without showing ellipses.


  #14  
Old July 17th, 2005, 12:21 PM
frizzle
Guest
 
Posts: n/a
Default Re: Break words, but not tags

Well thanks chung,
now i understand that i would be cutting 'inside' a character... Anyway
(might not sound professional) but for now i'm not nearly building
sites in non-'european' characters. Thanks for your explanation & time!
:)

  #15  
Old July 17th, 2005, 12:21 PM
Daniel Tryba
Guest
 
Posts: n/a
Default Re: Break words, but not tags

Chung Leong <chernyshevsky@hotmail.com> wrote:[color=blue]
> PHP strings are byte-oriented, it also means you can't use UTF-8,
> where one character can take up 2, 3 bytes.[/color]

That is what mbstring is for: mb_strlen() and mb_substr() will avoid
these problems.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.