지 20 문자열으로 잘라냅니다 미리 단어를 어떻게 해야 합니까?
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);
출력:
Hello here is a long ...
첫 번째 숫자 '20' 숫자 '를 변경하십시오 3' afaq 아래 20 단어 또는 전달하십시오 정보기술 (it) 을 매개변수입니다. 다음 볼 수 있는 방법을 통해 첫 번째 3 단어: (그래서 '를' 20 '변경하십시오 3' 변경하시려면 기본값입니다):
function first3words($s, $limit=3) {
return preg_replace('/((\w+\W*){'.($limit-1).'}(\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("")); # => ""
가장 가까운 공간을 위해 잘라냅니다 이전 대상 문자. 데모
$ str '-' 에서 문자열을 잘라낼 수
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);
}
<?php
$str = "this is a string that is just some text for you to test with";
print(truncateString($str, 20, false) . "\n");
print(truncateString($str, 22, false) . "\n");
print(truncateString($str, 24, true) . "\n");
print(truncateString($str, 26, true, " :)") . "\n");
print(truncateString($str, 28, true, "--") . "\n");
?>
this is a string tha...
this is a string that ...
this is a string that...
this is a string that is :)
this is a string that is--
분해선 () 사용합니다.
예) 의 문서.
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
참고로 분해선 제한값 기능을 가지고 있다. 그런 식으로 뭔가를 할 수 있도록
$message = implode(" ", explode(" ", $long_message, 20));
Regex 시도하시겠습니까.
뭐 필요한거라도 것이라고 20 일치 단어 (word 경계 또는 20).
그래서 (내 경우, regex 는 t # 39 이 날 수정하십시오 사악함이라 그러하매 isn& 정확한):
/(\w+\b){20}/
그리고 다음은 몇 가지 예를 들어 있는 regex php.
내 스스로 그 생성, 수정 아니라 그 이전 우려된다. 크레딧 마이그레이션된 karim79.
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;
}
>. 동적임 만들 때 자주 발생하는 문제입니다 >. (여기서 받는 컨텐츠가 웹 페이지 >. 데이터베이스에서, 콘텐츠 관리 >. 시스템 또는 소스 등 외부 >. RSS 피드) 는 해당 텍스트를 입력할 수 있습니다. >. 페이지 레이아웃 원인이 될 수 너무 길어 >. # 39, & # 39 를 break&;. >. >. 해결 방법은 잘라냅니다 한 텍스트 >. 맞도록 페이지에서. 이 >. 소박하지만 소리를 자주 결과 >. # 39 aren& 예상대로 말과 인해, t >. 마디였나 잘리는 dell. >. 부적절한 점.
아래 코드를 시도하시겠습니까
$text = implode(' ', array_slice(explode(' ', $text), 0, 32))
echo $text;
php 사용 토크니저 함수 스트라토크 () 에서 루프지
$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;
function truncate_words($string,$words=20) {
return preg_replace('/((\w+\W*){'.($words-1).'}(\w+))(.*)/', '${1}', $string);
}
또는
function truncate_words_with_ellipsis($string,$words=20,$ellipsis=' ...') {
$new = preg_replace('/((\w+\W*){'.($words-1).'}(\w+))(.*)/', '${1}', $string);
if($new != $string){
return $new.$ellipsis;
}else{
return $string;
}
}
다음은 내가 구현했습니까.
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;
}
난 또 https://partner. microsoft. 편의를 위해 주요 기능은 추가되었다. 지금까지 it 하사 협력했습니다 원활한 실행을. 덕분에 기존 솔루션 제공업체입니다.
function getShortString($string,$wordCount,$etc = true)
{
$expString = explode(' ',$string);
$wordsInString = count($expString);
if($wordsInString >= $wordCount )
{
$shortText = '';
for($i=0; $i < $wordCount-1; $i++)
{
$shortText .= $expString[$i].' ';
}
return $etc ? $shortText.='...' : $shortText;
}
else return $string;
}