Funksioni wordwrap

Funksioni wordwrap fut një karakter thyerjeje rreshti (parazgjedhur \n) pas çdo N karakteresh (parazgjedhur 75).

Sintaksa

phpwordwrap(string $string, int $width = 75, string $break = "\n", bool $cut_long_words = false): string

Shembull

Në këtë shembull, funksioni do ta ndajë stringun në 10 karaktere me thyerje:

php<?php
  $text = "Quick brown fox jumps over lazy dog";
  echo wordwrap($text, 10, "<br>\n");
?>

Rezultati i ekzekutimit të kodit:

phpQuick<br>
brown fox<br>
jumps over<br>
lazy dog

Shembull

Shembull me thyerje të detyruar të fjalëve të gjata:

php<?php
  $text = "Supercalifragilisticexpialidocious wordwrap example";
  echo wordwrap($text, 8, "<br>\n", true);
?>

Rezultati i ekzekutimit të kodit:

phpSupercal<br>
ifragil<br>
isticexp<br>
ialidoc<br>
ious<br>
wordwra<br>
p<br>
example

Shembull

Shembull me një URL të gjatë:

php<?php
  $url = "https://www.example.com/very/long/url/path/to/some/resource";
  echo wordwrap($url, 20, "<br>\n", true);
?>

Rezultati i ekzekutimit të kodit:

phphttps://www.exampl<br>
e.com/very/long/u<br>
rl/path/to/some/<br>
resource

Shihni gjithashtu

  • funksionin nl2br,
    i cili fut tag-un br pas karakterit të thyerjes së rreshtit