Function I wrote
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
Hey,
Me and a friend of mine saw a ton of flaws in the wordwrap() function built into PHP. I just finished writing my own that calculates values for letters and uses those when figuring when to break.
I'd like some opinions on the function.
You can download the function here: http://orangefoxgames.com/wordwrap2/
The function is called wordwrap2.
It accepts 4 parameters.
1. Text - this is the test you want to wrap
2. Number points - Number of points until break is inserted
3. Break value - What to insert into text when want to insert a break
4. Word flag - This is an optional parameter. If you set this to true, the function will never break during a word.
Sample usage:
<?php
$my_string = 'This is a long string lalalalalala............. What up???? Testing testing hi hi hi hi hi what do you thing?';
echo wordwrap2($my_string, 15, '<br/>');
?>
Any comments are appreciated!
- cherries
-
cherries
- Member since: Jun. 7, 2005
- Offline.
-
- Forum Stats
- Member
- Level 18
- Blank Slate
- DFox
-
DFox
- Member since: Aug. 9, 2003
- Offline.
-
- Forum Stats
- Member
- Level 30
- Blank Slate
At 6/18/06 09:49 PM, -cherries- wrote: what does it do?
It wraps text at a certain letter count.
But unlike the PHP function wordwrap() characters such as "." and "W" aren't counted as the same thing which makes the wordwrap2() function much more accurate.
- JeremysFilms
-
JeremysFilms
- Member since: Feb. 18, 2005
- Offline.
-
- Forum Stats
- Member
- Level 18
- Blank Slate
Yay, now I can use the function I had the idea of an 'contributed to' :D
- Craige
-
Craige
- Member since: Jul. 17, 2004
- Offline.
-
- Forum Stats
- Member
- Level 08
- Blank Slate
Well, for one, you are missing a bunch of ASCII characters; that is not good.
Seccond of all, for ($i = 0; $i < strlen($t); $i++) should be:
$len = strlen($t);
for ($i = 0; $i < $len; $i++)
When you are dooing a loop for every character in a string, it is good to use as few functions each time as possible. Because, say the string was 100 characters, and that is short, the function in the loop would be called 100 times. That can cause some serious load time problems.
Looks okay though. It would be nice if PHP's wordwrap compincated for character length.
// MustyWindows - Jump Through The Windows
// AmpFusion - Where Underground Becomes Mainstream
Neo Enterprise Technologies Coming soon.


