Connecting Tech Pros Worldwide Forums | Help | Site Map

Break words, but not tags

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


Chung Leong
Guest
 
Posts: n/a
#2: Jul 17 '05

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.


Janwillem Borleffs
Guest
 
Posts: n/a
#3: Jul 17 '05

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



frizzle
Guest
 
Posts: n/a
#4: Jul 17 '05

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.

Chung Leong
Guest
 
Posts: n/a
#5: Jul 17 '05

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.


frizzle
Guest
 
Posts: n/a
#6: Jul 17 '05

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.

Andy Hassall
Guest
 
Posts: n/a
#7: Jul 17 '05

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

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...)

Janwillem Borleffs
Guest
 
Posts: n/a
#9: Jul 17 '05

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



frizzle
Guest
 
Posts: n/a
#10: Jul 17 '05

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..

Janwillem Borleffs
Guest
 
Posts: n/a
#11: Jul 17 '05

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



frizzle
Guest
 
Posts: n/a
#12: Jul 17 '05

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?)

Chung Leong
Guest
 
Posts: n/a
#13: Jul 17 '05

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.


frizzle
Guest
 
Posts: n/a
#14: Jul 17 '05

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!
:)

Daniel Tryba
Guest
 
Posts: n/a
#15: Jul 17 '05

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.

Closed Thread