473,815 Members | 3,968 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help with word wrap function

3 New Member
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=>$l ine) {
$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 1161

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

Similar topics

6
6156
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 want...and some act weird as well. I want, If I say break at 150 chars then the function will break at the nearest space to 150 ie. it will actually break at the word boundry. And if theres a url it will not break inside a URL, it will leave URLS alone....
8
3141
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
15320
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 "locate.replace". I can do all this if the html page containing the script originates on the client machine, but don't know how to get access to a client-side temp file when the same html page containing the script is on a web host. How do I get access...
10
72707
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
1576
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 be displayed. the table is set at 500px wide, with each column being a width that adds up to 500px. so far so simple. the text that will display in the middle column is being pulled from a database. i have no way of knowing how long, or what...
3
3393
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; } * html .clearfix { height: 1%; }
1
8772
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 advance. Cheers!
10
1213
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 versions and what I want to do is to create a single confirmation page which is responsible to send the forms values. What I want to do is that when the arabic form is submitted the confirmation echos arabic succesful note and when english an...
3
7082
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 Len(Apl) Leng Then i = 0 Top: If i = Leng Then cvtWordWrap = Apl: Exit Function
0
9735
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9610
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10670
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10408
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10426
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9225
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7686
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...

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.