问题
I have a description field in my MySQL database, and I access the database on two different pages, one page I display the whole field, but on the other, I just want to display the first 50 characters. If the string in the description field is less than 50 characters, then it won\'t show ... , but if it isn\'t, I will show ... after the first 50 characters.
Example (Full string):
Hello, this is the first example, where I am going to have a string that is over 50 characters and is super long, I don\'t know how long maybe around 1000 characters. Anyway this should be over 50 characters now ...
Exmaple 2 (first 50 characters):
Hello, this is the first example, where I am going ...
回答1:
The PHP way of doing this is simple:
$out = strlen($in) > 50 ? substr($in,0,50)."..." : $in;
But you can achieve a much nicer effect with this CSS:
.ellipsis {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Now, assuming the element has a fixed width, the browser will automatically break off and add the ...
for you.
回答2:
You can achieve the desired trim in this way too:
mb_strimwidth("Hello World", 0, 10, "...");
Where:
Hello World
: the string to trim.0
: number of characters from the beginning of the string.10
: the length of the trimmed string....
: an added string at the end of the trimmed string.
This will return Hello W...
.
Notice that 10 is the length of the truncated string + the added string!
Documentation: http://php.net/manual/en/function.mb-strimwidth.php
回答3:
Use wordwrap()
to truncate the string without breaking words if the string is longer than 50 characters, and just add ...
at the end:
$str = $input;
if( strlen( $input) > 50) {
$str = explode( "\n", wordwrap( $input, 50));
$str = $str[0] . '...';
}
echo $str;
Otherwise, using solutions that do substr( $input, 0, 50);
will break words.
回答4:
if (strlen($string) <=50) {
echo $string;
} else {
echo substr($string, 0, 50) . '...';
}
回答5:
<?php
function truncate($string, $length, $stopanywhere=false) {
//truncates a string to a certain char length, stopping on a word if not specified otherwise.
if (strlen($string) > $length) {
//limit hit!
$string = substr($string,0,($length -3));
if ($stopanywhere) {
//stop anywhere
$string .= '...';
} else{
//stop on a word.
$string = substr($string,0,strrpos($string,' ')).'...';
}
}
return $string;
}
?>
I use the above code snippet many-a-times..
回答6:
I use this solution on my website. If $str is shorter, than $max, it will remain unchanged. If $str has no spaces among first $max characters, it will be brutally cut at $max position. Otherwise 3 dots will be added after the last whole word.
function short_str($str, $max = 50) {
$str = trim($str);
if (strlen($str) > $max) {
$s_pos = strpos($str, ' ');
$cut = $s_pos === false || $s_pos > $max;
$str = wordwrap($str, $max, ';;', $cut);
$str = explode(';;', $str);
$str = $str[0] . '...';
}
return $str;
}
回答7:
This will return a given string with ellipsis based on WORD count instead of characters:
<?php
/**
* Return an elipsis given a string and a number of words
*/
function elipsis ($text, $words = 30) {
// Check if string has more than X words
if (str_word_count($text) > $words) {
// Extract first X words from string
preg_match("/(?:[^\s,\.;\?\!]+(?:[\s,\.;\?\!]+|$)){0,$words}/", $text, $matches);
$text = trim($matches[0]);
// Let's check if it ends in a comma or a dot.
if (substr($text, -1) == ',') {
// If it's a comma, let's remove it and add a ellipsis
$text = rtrim($text, ',');
$text .= '...';
} else if (substr($text, -1) == '.') {
// If it's a dot, let's remove it and add a ellipsis (optional)
$text = rtrim($text, '.');
$text .= '...';
} else {
// Doesn't end in dot or comma, just adding ellipsis here
$text .= '...';
}
}
// Returns "ellipsed" text, or just the string, if it's less than X words wide.
return $text;
}
$description = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quibusdam ut placeat consequuntur pariatur iure eum ducimus quasi perferendis, laborum obcaecati iusto ullam expedita excepturi debitis nisi deserunt fugiat velit assumenda. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Incidunt, blanditiis nostrum. Nostrum cumque non rerum ducimus voluptas officia tempore modi, nulla nisi illum, voluptates dolor sapiente ut iusto earum. Esse? Lorem ipsum dolor sit amet, consectetur adipisicing elit. A eligendi perspiciatis natus autem. Necessitatibus eligendi doloribus corporis quia, quas laboriosam. Beatae repellat dolor alias. Perferendis, distinctio, laudantium? Dolorum, veniam, amet!';
echo elipsis($description, 30);
?>
回答8:
<?php
$string = 'This is your string';
if( strlen( $string ) > 50 ) {
$string = substr( $string, 0, 50 ) . '...';
}
That's it.
回答9:
$string = "Hello, this is the first example, where I am going to have a string that is over 50 characters and is super long, I don't know how long maybe around 1000 characters. Anyway this should be over 50 characters know...";
if(strlen($string) >= 50)
{
echo substr($string, 50); //prints everything after 50th character
echo substr($string, 0, 50); //prints everything before 50th character
}
回答10:
You can use str_split() for this
$str = "Hello, this is the first example, where I am going to have a string that is over 50 characters and is super long, I don't know how long maybe around 1000 characters. Anyway this should be over 50 characters know...";
$split = str_split($str, 50);
$final = $split[0] . "...";
echo $final;
回答11:
// this method will return the break string without breaking word
$string = "A brown fox jump over the lazy dog";
$len_required= 10;
// user strip_tags($string) if string contain html character
if(strlen($string) > 10)
{
$break_str = explode( "\n", wordwrap( $string , $len_required));
$new_str =$break_str[0] . '...';
}
// other method is use substr
来源:https://stackoverflow.com/questions/11434091/add-if-string-is-too-long-php