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

Break words, but not tags

P: n/a
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);
};

Jul 17 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
"frizzle" <ph********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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))


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.
Jul 17 '05 #2

P: n/a
frizzle wrote:
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...


The following might be helpful (not fully tested):

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

Jul 17 '05 #3

P: n/a
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.

Jul 17 '05 #4

P: n/a
"frizzle" <ph********@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
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.


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.
Jul 17 '05 #5

P: n/a
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.

Jul 17 '05 #6

P: n/a
On Sat, 26 Mar 2005 19:10:47 -0500, "Chung Leong" <ch***********@hotmail.com>
wrote:
<div style="overflow: hidden; overflow: hidden; text-overflow: ellipsis; ">


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

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #7

P: n/a
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...)

Jul 17 '05 #8

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


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

Jul 17 '05 #9

P: n/a
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..

Jul 17 '05 #10

P: n/a
frizzle wrote:
Could you maybe somehow show me an example of how i could expand this
function's functionality?


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

Jul 17 '05 #11

P: n/a
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?)

Jul 17 '05 #12

P: n/a
"frizzle" <ph********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hmmm, i don't understand the last part of your reply,
but i do understand the first part.
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.
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... ;)


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

P: n/a
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!
:)

Jul 17 '05 #14

P: n/a
Chung Leong <ch***********@hotmail.com> wrote:
PHP strings are byte-oriented, it also means you can't use UTF-8,
where one character can take up 2, 3 bytes.


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

Jul 17 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.