添加…如果string太长PHP
我在我的MySQL数据库中有一个描述字段,我在两个不同的页面上访问数据库,其中一个页面显示整个字段,但是另一个页面只显示前50个字符。 如果说明字段中的stringless于50个字符,则不会显示…,但如果不是,我会在前50个字符后显示。
示例(完整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 ...
例2(前50个字符):
Hello, this is the first example, where I am going ...
PHP的做法很简单:
$out = strlen($in) > 50 ? substr($in,0,50)."..." : $in;
但是你可以用这个CSS获得更好的效果:
.ellipsis { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
现在,假设元素具有固定的宽度,浏览器将自动中断并添加...
为您。
您也可以通过这种方式实现所需的修剪:
mb_strimwidth("Hello World", 0, 10, "...");
哪里:
-
Hello World
:修剪的string。 -
0
:string开头的字符数。 -
10
:修剪的string的长度。 -
...
:修剪的string末尾添加的string。
这将返回Hello W...
请注意,10是截断string+添加的string的长度!
文档: http : //php.net/manual/en/function.mb-strimwidth.php
如果string长度超过50个字符,请使用wordwrap()
截断string, 而不要打断单词 ,只需在末尾添加...
:
$str = $input; if( strlen( $input) > 50) { $str = explode( "\n", wordwrap( $input, 50)); $str = $str[0] . '...'; } echo $str;
否则,使用substr( $input, 0, 50);
解决schemesubstr( $input, 0, 50);
会打破话语。
if (strlen($string) <=50) { echo $string; } else { echo substr($string, 0, 50) . '...'; }
<?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; } ?>
我使用上面的代码片断多次。
<?php $string = 'This is your string'; if( strlen( $string ) > 50 ) { $string = substr( $string, 0, 50 ) . '...'; }
而已。
$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 }
我在我的网站上使用这个解决scheme。 如果$ str比$ max短,它将保持不变。 如果$ str在$ max字符之间没有空格,它将在$ max位置被残酷地切断。 否则在最后一个单词之后将会添加3个点。
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; }
你可以使用str_split()
来做到这一点
$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;
这将返回给定的string与基于WORD计数,而不是字符的省略号:
<?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); ?>