Jump to content
php.lv forumi

apgriezt garus vaardus?


andain

Recommended Posts

lieta taada, ka vaidzeetu garos vardus vienk saiisinat:

 

aaaaaaaaaaaaaaaaaaaaaaaaaaa
->
aaaaaaaaaa...

 

izmeklejos google par wordwrapu - nedereeja nekas. pec tam iemegjinaju preg_replace:

 

<?php

 

function wordLimit($string, $length = 50, $ellipsis = '...') {

  return count($words = preg_split('/\s+/', ltrim($string), $length + 1)) > $length ?

      rtrim(substr($string, 0, strlen($string) - strlen(end($words)))) . $ellipsis :

      $string;

}

 

function stringLimit($string, $length = 50, $ellipsis = '...') {

  return strlen($fragment = substr($string, 0, $length + 1 - strlen($ellipsis))) < strlen($string) + 1 ?

      preg_replace('/\s*\S*$/', '', $fragment) . $ellipsis : $string;

}

 

echo wordLimit('  You can limit a string to only so many words.', 6);

// Output: "You can limit a string to..."

echo stringLimit('Or you can limit a string to a certain amount of characters.', 32);

// Output: "Or you can limit a string to..."

 

?>

bet tiklidz meginaju pie stringLimit pielikt kadu mainigo, piemeeram,
$text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

echo stringLimit($text, 32)

 

lapaa paraadas tikai ...

 

varbut kads var palidzet vai arii ieteikt kko vienkarshaku?

 

thx

Link to comment
Share on other sites

×
×
  • Create New...