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 destroying
html tags which wordwrap has a tendency of doing! ))
What I want to do is add that line break so that it DOES force a
line wrap - but I am not sure where to insert it in the function
Can anyone suggest which line of the following function to change ? - /* htmlwrap() is a function which wraps HTML by breaking long words and
-
* preventing them from damaging your layout. This function will NOT
-
* insert <br /> tags every "width" characters as in the PHP wordwrap()
-
* function.
-
*/
-
-
function htmlwrap($str, $width = 70, $break = "\n", $nobreak = "") {
-
-
// Split HTML content into an array delimited by < and >
-
// The flags save the delimeters and remove empty variables
-
$content = preg_split("/([<>])/", $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
-
-
// Transform protected element lists into arrays
-
$nobreak = explode(" ", strtolower($nobreak));
-
-
// Variable setup
-
$intag = false;
-
$innbk = array();
-
$drain = "";
-
-
// List of characters it is "safe" to insert line-breaks at
-
// It is not necessary to add < and > as they are automatically implied
-
$lbrks = "/?!%)-}]\\\"':;&";
-
-
// Is $str a UTF8 string?
-
$utf8 = (preg_match("/^([\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*$/", $str)) ? "u" : "";
-
-
while (list(, $value) = each($content)) {
-
switch ($value) {
-
-
// If a < is encountered, set the "in-tag" flag
-
case "<": $intag = true; break;
-
-
// If a > is encountered, remove the flag
-
case ">": $intag = false; break;
-
-
default:
-
-
// If we are currently within a tag...
-
if ($intag) {
-
-
// Create a lowercase copy of this tag's contents
-
$lvalue = strtolower($value);
-
-
// If the first character is not a / then this is an opening tag
-
if ($lvalue{0} != "/") {
-
-
// Collect the tag name
-
preg_match("/^(\w*?)(\s|$)/", $lvalue, $t);
-
-
// If this is a protected element, activate the associated protection flag
-
if (in_array($t[1], $nobreak)) array_unshift($innbk, $t[1]);
-
-
// Otherwise this is a closing tag
-
} else {
-
-
// If this is a closing tag for a protected element, unset the flag
-
if (in_array(substr($lvalue, 1), $nobreak)) {
-
reset($innbk);
-
while (list($key, $tag) = each($innbk)) {
-
if (substr($lvalue, 1) == $tag) {
-
unset($innbk[$key]);
-
break;
-
}
-
}
-
$innbk = array_values($innbk);
-
}
-
}
-
-
// Else if we're outside any tags...
-
} else if ($value) {
-
-
// If unprotected...
-
if (!count($innbk)) {
-
-
// Use the ACK (006) ASCII symbol to replace all HTML entities temporarily
-
$value = str_replace("\x06", "", $value);
-
preg_match_all("/&([a-z\d]{2,7}|#\d{2,5});/i", $value, $ents);
-
$value = preg_replace("/&([a-z\d]{2,7}|#\d{2,5});/i", "\x06", $value);
-
-
// Enter the line-break loop
-
do {
-
$store = $value;
-
-
// Find the first stretch of characters over the $width limit
-
if (preg_match("/^(.*?\s)?([^\s]{".$width."})(?!(".preg_quote($break, "/")."|\s))(.*)$/s{$utf8}", $value, $match)) {
-
-
if (strlen($match[2])) {
-
// Determine the last "safe line-break" character within this match
-
for ($x = 0, $ledge = 0; $x < strlen($lbrks); $x++) $ledge = max($ledge, strrpos($match[2], $lbrks{$x}));
-
if (!$ledge) $ledge = strlen($match[2]) - 1;
-
-
// Insert the modified string
-
$value = $match[1].substr($match[2], 0, $ledge + 1).$break.substr($match[2], $ledge + 1).$match[4];
-
}
-
}
-
-
// Loop while overlimit strings are still being found
-
} while ($store != $value);
-
-
// Put captured HTML entities back into the string
-
foreach ($ents[0] as $ent) $value = preg_replace("/\x06/", $ent, $value, 1);
-
}
-
}
-
}
-
-
// Send the modified segment down the drain
-
$drain .= $value;
-
}
-
-
// Return contents of the drain
-
return $drain;
-
}
-
-
?>
7 4065
From my first post:
( the script safely wraps long words without destroying
html tags which wordwrap has a tendency of doing! ))
Anyone know how to insert this line break ?
@jeddiki
Ya, but you continue as
What I want to do is add that line break so that it DOES force a
line wrap
So I don't get it...do you want to break HTML or not?
This function htmlwrap() does not add a line break - that is what it says in the description - but that is exactly why I want to modify it
I want to have a line break inserted at the end of 70 characters,
unless doing so will break a html tag - it that case, keep going
until outside the tags - then insert the line break.
This function htmlwrap() is very close to what I want because it carefully
leaves the hyperlinks ( and any other html tags ) intact - it won't touch them. Wordwrap() on the other hand can not do this - it wrecks my hyperlinks.
So what I want to do is modify this htmlwrap so that it DOES put the
line break in and force a wrap without damaging my hyperlinks.
Can you see a way to modify this function ?
Thanks
Well right in the definition it gives you away to put your own type of break in instead of the default "\n"
What happens if you call the function with the third param as "<br/>" ?
Dan
Well Dan
That is a good idea,
and to be honest, I would have been petty embarrassed
if it had worked ;-)
Although, of course I wanted it to because I want to close the problem.
Unfortunately, it did not make any difference. :(
Hi all,
Last week I emailed the programmer who wrote this script
to see if he could help me put a line break into it
and force the wrap.
Unfortunately he has not replied, so I am still looking for a solution :(
It is not a very long script, but it is a bit too complicated for me.
Has anyone noticed how I can get my forced wrap ?
Thanks.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Richard Cornford |
last post by:
For the last couple of months I have been trying to get the next round
of updates to the FAQ underway and been being thwarted by a heavy
workload (the project I am working on has to be finished an...
|
by: comp.lang.tcl |
last post by:
I wrote this PHP function in the hopes that it would properly use a TCL
proc I wrote about 4 years ago:
if (!function_exists('proper_case')) {
/**
* Ths function will convert a string into a...
|
by: msnews.microsoft.com |
last post by:
Hi
i am using User32.dll in Visual stdio 2005.
public static extern long SetActiveWindow(long hwnd);
public static extern long keybd_event(byte bVk, byte bScan, long dwFlags,
|
by: sonald |
last post by:
Hi,
Can anybody tell me how to change the text delimiter in FastCSV Parser
?
By default the text delimiter is double quotes(")
I want to change it to anything else... say a pipe (|)..
can anyone...
|
by: mIDO |
last post by:
Hello,
I have developed a script that change the font size in css, but only
the attribute "font-size" on the body tag, not change the entire
active stylesheet.
When i click to change the size...
|
by: Sam Samson |
last post by:
Greeetings All ..
For my users convenience I have mapped function keys F2 .. F12 to change
the tabs on the Tabcontrol.
Works like a charm <ominous musicuntil one hits F8</ominous music>
...
|
by: Daniel T. |
last post by:
The function below does exactly what I want it to (there is a main to
test it as well.) However, I'm curious about ideas of making it better.
Anyone interested in critiquing it?
void formatText(...
|
by: lovecreatesbea... |
last post by:
It takes mu so time to finish this C source code line count function.
What do you think about it?
/
*******************************************************************************
* Function ...
|
by: CFFAN |
last post by:
<h3>Wrap Example</h3>
<cfset inputText1="Inserts line break at the location of the first white space character (such as a space, tab, or new line) before the specified limit on a line. If a line has...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |