Files
selima-perl/lib/php/monica/spltline.inc.php
2026-03-10 21:31:43 +08:00

47 lines
1.3 KiB
PHP

<?php
// File name: spltline.inc.php
// Description: PHP subroutine to split long lines
// Date: 2004-07-04
// Author: imacat <imacat@pristine.com.tw>
// Copyright: Copyright (C) 2004-2007 Pristine Communications
// split_long_lines: Split long lines (English only)
function split_long_lines($source, $line_width)
{
$pos = 0;
$result = "";
while (preg_match("/^(\S+[^\n\S]*|[^\n\S]+|\n+)/s", $source, $m)) {
$piece = $m[1];
// Text token
if (preg_match("/^\S/", $piece)) {
// Split line
if ($pos + strlen($piece) > $line_width) {
$result .= "\n";
$pos = 0;
}
$result .= $piece;
$pos += strlen($piece);
// Space token -- skip if over $line_width
} elseif (preg_match("/^[^\n\S]/", $piece)) {
// Split line
if ($pos + strlen($piece) > $line_width) {
$result .= "\n";
$pos = 0;
} else {
$result .= $piece;
$pos += strlen($piece);
}
// New-line token -- add it and reset the position
} else {
$result .= $piece;
$pos = 0;
}
$source = substr($source, strlen($piece));
}
return $result;
}
?>