PHP nasıl 20 kelime sonra bir dize kesecek miyim?
function limit_text($text, $limit) {
if (str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}
echo limit_text('Hello here is a long sentence blah blah blah blah blah hahahaha haha haaaaaa', 5);
Çıkışlar:
Hello here is a long ...
ilk 20 kelime almak için 19'a "2" olarak değiştirin. Bu bir ilk 3 kelime alır:
function first3words($s) {
return preg_replace('/((\w+\W*){2}(\w+))(.*)/', '${1}', $s);
}
var_dump(first3words("hello yes, world wah ha ha")); # => "hello yes, world"
var_dump(first3words("hello yes,world wah ha ha")); # => "hello yes,world"
var_dump(first3words("hello yes world wah ha ha")); # => "hello yes world"
var_dump(first3words("hello yes world")); # => "hello yes world"
var_dump(first3words("hello yes world.")); # => "hello yes world"
var_dump(first3words("hello yes")); # => "hello yes"
var_dump(first3words("hello")); # => "hello"
var_dump(first3words("a")); # => "a"
var_dump(first3words("")); # => ""
A common problem when creating dynamic web pages (where content is sourced from a database, content management system or external source such as an RSS feed) is that the input text can be too long and cause the page layout to 'break'.
One solution is to truncate the text so that it fits on the page. This sounds simple, but often the results aren't as expected due to words and sentences being cut off at inappropriate points.
kullanmak explode().
Dokümanlar Örnek.
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
patlayabilir notu bir sınır işlevi vardır. Böylece gibi bir şey yapabileceğini
$message = implode(" ", explode(" ", $long_message, 20));
Regex deneyin.
Sen 20 kelime (ya da 20 kelime sınırları) maç olacak bir şey gerekir.
Yani (benim regex bu doğru değildir eğer öyleyse beni düzeltin korkunç):
/(\w+\b){20}/
Ve burada some examples of regex in php.
bir döngüde PHP tokenizer işlevi strtok() kullanın.
$token = strtok($string, " "); // we assume that words are separated by sapce or tab
$i = 0;
$first20Words = '';
while ($token !== false && $i < 20) {
$first20Words .= $token;
$token = strtok(" ");
$i++;
}
echo $first20Words;
动静 能量 'nin cevap dayalı:
function truncate_wveyads($string,$wveyads=20) {
return preg_replace('/((\w+\W*){'.($wveyads-1).'}(\w+))(.*)/', '${1}', $string);
}
veya
function truncate_wveyads_with_ellipsis($string,$wveyads=20,$ellipsis=' ...') {
$new = preg_replace('/((\w+\W*){'.($wveyads-1).'}(\w+))(.*)/', '${1}', $string);
if($new != $string){
return $new.$ellipsis;
}else{
return $string;
}
}
Onun değil, benim kendi oluşturma, önceki mesajların onun bir değişiklik. Credit karim79 gider ...! teşekkürler yaar ...
function limit_text($text, $limit) {
$strings = $text;
if (strlen($text) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
if(sizeof($pos) >$limit)
{
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}
return $text;
}
Hedef karakter boşluk önceki en yakın keser.
<?php
$str = "this is a string that is just some text for you to test with";
print(truncateString($str, 20, true) . "\n");
print(truncateString($str, 22, true) . "\n");
print(truncateString($str, 24, true) . "\n");
print(truncateString($str, 26, true, " :)") . "\n");
print(truncateString($str, 28, true, "--") . "\n");
function truncateString($str, $chars, $to_space, $replacement="...") {
if($chars > strlen($str)) return $str;
$str = substr($str, 0, $chars);
$space_pos = strrpos($str, " ");
if($to_space && $space_pos >= 0) {
$str = substr($str, 0, strrpos($str, " "));
}
return($str . $replacement);
}
?>
/* OUTPUT
this is a string...
this is a string that...
this is a string that...
this is a string that is :)
this is a string that is--
*/
Burada uygulanan budur.
function summaryMode($text, $limit, $link) {
if (str_word_count($text, 0) > $limit) {
$numwords = str_word_count($text, 2);
$pos = array_keys($numwords);
$text = substr($text, 0, $pos[$limit]).'... <a href="'.$link.'">Read More</a>';
}
return $text;
}
Gördüğünüz gibi karim79 cevabını off dayanır, değiştirilmesi gereken tüm olmasıydı deyimi de kelimelerin karakter değil karşı kontrol etmek gerekirse.
Ben de kolaylık için ana işlevi bir link ekledi. Şimdiye kadar sorunsuz çalıştı HSA. Orijinal çözüm sağlayıcı teşekkürler.
Burada kullandığım biri:
$truncate = function( $str, $length ) {
if( strlen( $str ) > $length && false !== strpos( $str, ' ' ) ) {
$str = preg_split( '/ [^ ]*$/', substr( $str, 0, $length ));
return htmlspecialchars($str[0]) . '…';
} else {
return htmlspecialchars($str);
}
};
return $truncate( $myStr, 50 );