Core Function WordWrap

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
m (1 revision)
(Example)
 
(4 intermediate revisions by one user not shown)
Line 15: Line 15:
 
==== width ====
 
==== width ====
  
The number of characters at which the string will be wrapped.  
+
The number of characters at which the string will be wrapped.
 +
 
 +
If 0 then it will wrap at every individual word.
 +
 
 +
If lower than 0 then it will be set to 0.
  
 
==== break ====
 
==== break ====
  
 
Optional; The line is broken using this string.  
 
Optional; The line is broken using this string.  
 +
 +
Default: "\n"
  
 
==== cut ====
 
==== cut ====
  
 
If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).  
 
If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).  
 +
 +
Default: false
  
 
=== Return Value ===
 
=== Return Value ===
Line 40: Line 48:
 
$text = "The quick brown fox jumped over the lazy dog.";
 
$text = "The quick brown fox jumped over the lazy dog.";
 
$newtext = wordwrap($text, 20, "<br />\n");
 
$newtext = wordwrap($text, 20, "<br />\n");
 
 
echo $newtext;
 
echo $newtext;
 +
// PRINTS
 +
// The quick brown fox<br />
 +
// jumped over the lazy<br />
 +
// dog.
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
Breaking words apart
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 
$text = "A very long woooooooooooord.";
 
$text = "A very long woooooooooooord.";
 
$newtext = wordwrap($text, 8, "\n", true);
 
$newtext = wordwrap($text, 8, "\n", true);
 
 
echo "$newtext\n";
 
echo "$newtext\n";
 +
// PRINTS
 +
// A very
 +
// long
 +
// wooooooo
 +
// ooooord.
 +
</syntaxhighlight>
 +
 +
If the break is a single char and you are not breaking any words apart then it will simply place the break in the spaces it can find like so.
 +
 +
<syntaxhighlight lang="sputnik">
 +
$text = "The quick brown fox jumped over the lazy dog.";
 +
say wordwrap($text, 5, ",");
 +
// PRINTS
 +
// The,quick,brown,fox,jumped,over,the,lazy,dog.
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 20:31, 19 September 2015

WordWrap( <str>, <width>, <break>, <cut> )

Contents

Description

Wraps a string to a given number of characters.

Parameters

str

The input string.

width

The number of characters at which the string will be wrapped.

If 0 then it will wrap at every individual word.

If lower than 0 then it will be set to 0.

break

Optional; The line is broken using this string.

Default: "\n"

cut

If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).

Default: false

Return Value

Success: Returns the given string wrapped at the specified length.

Failure: Returns empty string.

Remarks

None.

Example

$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
echo $newtext;
// PRINTS
// The quick brown fox<br />
// jumped over the lazy<br />
// dog.

Breaking words apart

$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", true);
echo "$newtext\n";
// PRINTS
// A very
// long
// wooooooo
// ooooord.

If the break is a single char and you are not breaking any words apart then it will simply place the break in the spaces it can find like so.

$text = "The quick brown fox jumped over the lazy dog.";
say wordwrap($text, 5, ",");
// PRINTS
// The,quick,brown,fox,jumped,over,the,lazy,dog.
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox