473,400 Members | 2,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,400 software developers and data experts.

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);
};

Jul 17 '05 #1
14 1694
"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
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
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
"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
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
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
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
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: knoak | last post by:
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...
5
by: Kathryn | last post by:
Good morning! I am having a problem with a span. I have items, of which I only want to show the first X characters on the screen. If the user prints the page, I want the entire item to print....
9
by: opt_inf_env | last post by:
Hello, I have the following fragment in my html document: <h2>Text 1</h2> <small>Text 2</small> If I see this fragment with browser I see empty line between text1 and text2. Do you know...
7
by: Abraham Luna | last post by:
how do i stop the dynamic validators from breaking explorer if i use a dynamic validator and move to a different control it breaks explorer and i can type in the page when i'm not supposed to....
3
by: Papajo | last post by:
  I'd like to know how to add break tags "<br>" inbetween entry's in an input form, an example is if the word "yada yada" was entered you would wind up with y<br>a<br>d<br>a<br> y<br>a<br>d<br>a ...
35
by: shapper | last post by:
Hello, I am creating a search engine which searches keywords inside SQL database and displays the results in a web page. In the results web page I would like to give a grey background to the...
3
by: gomsi | last post by:
hi, i have a paragraph where i have to insert <markand </marktags to highlight the required words.i am implementing it in c++ i am currently doing it as follows.. i take the corresponding...
7
by: jeddiki | last post by:
Hi, I am using a function called htmlwrap() which states that it does NOT add a "<br>" to the 70 character line so that it forces a line wrap. ( the script safely wraps long words without...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.