You may use these two and not have to bother what version of PHP your script runs on.
Note that you may have to edit wordwrap if you need to chop exceedingly long words (this code allows long lines in this case). Please let me know if you use/optimize this. HTH Bogdan ------------------------------ // Function to determine if the current version of PHP is // at least the one specified as a string in the parameter. // This is needed because string comparison isn't always accurate: // "4.0.2">"4.0.12" as strings, although that's not true when // speaking of comparing versions. // Usage example: // if (!isphpver("4.0.2")) // { // [do something] // } // Bogdan Stancescu <[EMAIL PROTECTED]>, November 2001 function isphpver($minver) { $itis=2; // That is, undecided $minimum=explode(".",$minver); $current=explode(".",phpversion()); for ($i=0;(($i<sizeof($current)) && ($i<sizeof($minimum)));$i++) { if ($current[$i]>$minimum[$i]) { $itis=true; // In this case, we have a winner break; } if ($current[$i]<$minimum[$i]) // > { $itis=false; // In this case, we have a loser break; } } if ($itis==2) // This would only happen if all the common version // components are identical. But there are may be // differences: // Example 1: comparing "4.0.1" with "4.0" would be // identical for now, but the condition // is satisfied; // Example 2: comparing "4.0" with "4.0.1" - identical // for now, but the condition is NOT // satisfied { if (sizeof($current)>=sizeof($minimum)) { $itis=true; } else { // Ok, only one more chance: if for example the user // specified "4.0.0" and phpversion returned "4.0". for ($i=sizeof($current)-1;$i<sizeof($minimum);$i++) // > { if ($minimum[$i]) { $itis=false; } } if ($itis==2) { $itis=true; } } } return($itis); } // Bogdan Stancescu <[EMAIL PROTECTED]>, November 2001 if (!isphpver("4.0.2")) { // Word-wrap $string at $width chars // Already available in PHP 4.0.2 and later function wordwrap($string,$width=75) { // So, this is how we're going to go about wrapping the incoming // text: // We first break the text into natural lines: $tm_str=explode("\n",$string); for ($i=0;$i<sizeOf($tm_str);$i++) { // Then we move each line into a temporary string we'll start // chopping until we finish it. $temp_str=$tm_str[$i]; $line_st=""; while ($temp_str) { // Now let's see if the incoming string (or what's left of it) // isn't already shorter than the desired length. if (($line_len=strlen($temp_str))>$width) { // If it isn't, we'll just chop a piece of the ideal width // for starters. $wrapped_st=substr($temp_str,0,$width); // We'll start looking for a space in order to break the // line there. // (searching backwards!) if ($wrapping_len=strrpos($wrapped_st," ")) { // If there is one (it usually is) then we'll retrieve its // position and use it $line_len=$wrapping_len; } else { // Otherwise (i.e. a LOOONG word) then we'll just have to // look where it ends and use that. $line_len=strpos($temp_str," "); } } // Now that we have the proper line ending we'll chop the // temporary string and add the chopped // part to the final (wrapped) string, along with a line // break. // Obviously, we'll do that in // reversed order than described here. $line_st.=substr($temp_str,0,$line_len)."\n"; $temp_str=substr($temp_str,$line_len); // Making sure we don't leave spaces at the beginning of the // next line if (substr($temp_str,0,1)==" ") { $temp_str=substr($temp_str,1); } } // while // We need this for incoming empty lines: if (!strlen($line_st)) { $final_st.="\n"; } else { $final_st.=$line_st; } } // for return($final_st); } // function } // if phpver ------------------------------ Dominik Roettsches wrote: > Hi there, > > I've been searching for a PHP implementation of the GNU textformatting tool > fmt (http://www.gnu.org/software/textutils/textutils.html) which provides an > almost optimal linebreak for your text. > > I'm not skilled in enough in C programming to implement such a feature for > PHP but I'd really appreciate having this algorithm available in a simple > PHP function. > > My question is, if there's anyone who is interested in implementing this for > PHP, so that we can see it in one of the next releases? > > I think, the function would be very useful to do some pretty reformatting of > text when for example sending mail or in general doing text formatted > output. > > Thanks for your replies in advance, > > Dominik > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]