473,406 Members | 2,217 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,406 software developers and data experts.

need help with word wrap function

I've been messing about with a function to get a text block to not blow its sides.

I need it to be 29 characters wide and 3 lines high only. I have'nt had much success with the standard php functions from php.net and have tried variations myself, but cannot get it to work.


[PHP]
function justify($text, $width, $break) {
$marker = "__$%@random#$()__";

// lines is an array of lines containing the word-wrapped text
$wrapped = wordwrap($text, $width, $marker);
$lines = explode($marker, $wrapped);
$result = "";
foreach ($lines as $line_index=>$line) {
$line = trim($line);
$words = explode(" ", $line);
$words = array_map("trim", $words);
$wordcount = count($words);
$wordlength = strlen(implode("", $words));
if (3*$wordlength < 2*$width) {
// don't touch lines shorter than 2/3 * width
continue;
}
$spaces = $width - $wordlength;
$index = 0;
do {
$words[$index] = $words[$index] . " ";
$index = ($index + 1) % ($wordcount - 1);
$spaces--;
} while ($spaces>0);
$lines[$line_index] = implode("", $words);
}
return implode($break, $lines);
}
[/PHP]


This is my base code and need to modify it to only output 3 lines, no matter what.
May 2 '07 #1
0 1142

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: leegold2 | last post by:
I have been looking for a php word wrap function. I know there's an official PHP function but I've tried that and many of the functions contributed on that php.net page and none do what I...
8
by: news.pandora.be | last post by:
I want to display a string in a table but when a word in the string is longer then 40 letters it won't wrap. Does anyone know how I can fix this ? Thanx Wannes
26
by: Don | last post by:
I'm writing an html page with some JavaScript that reads a web page into a client-side temp file, then reformats it, then submits that same file as a URL to the browser for display, via...
10
by: Lorenzo Thurman | last post by:
I have a table cell that I want to wrap text inside of. I've tried both hard and soft wrap, but Firefox refuses to obey. IE 6&7 handle the wrap just fine. Does anyone know how I can fix this?
8
by: Santos L Halper | last post by:
i cant believe that im the only person that has ever run into this problem. im using a very simple table to layout a page. the table has 3 columns, with the middle column being where the data will...
3
by: BmB | last post by:
My CSS hack displays well in all browser but IE 7. What is different in IE 7 that it displays boxes in overflow? Here is the CSS script: @media all { ..clearfix { display: inline-block; } ...
1
by: winston.heng | last post by:
Hi, Thanks for reading this posting. I have been cracking my head on solving the infinite loop when i call the following section code. Any help or advise is greatly appreciated =D Thanks in...
10
by: shror | last post by:
Hi every body, I need your help in a problem am facing, I have two forms on my website which when submitted they goto single page (confirmation.php) and the two forms are for English and Arabic...
3
by: christianlott1 | last post by:
I needed this for address labels that wouldn't wrap. Couldn't find the function so I made one. Splits at space. Function cvtWordWrap(Apl As String, Leng As Long) As String Dim i As Long If...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.