从长[关闭]做短串
问题描述:
i am new in web development field .
can someone help me how i can get a short string from an article body if its longer then 150 chars. thanks in advance
$title = "Post Title";
$body = "Lorem ipsum dolor sit amet, consectetur .adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
我是网络开发领域的新手。 p>
有人可以帮助我如何从文章正文得到一个短字符串,如果它超过150个字符。 提前预先 p>
$ title =“Post Title”;
$ body =“Lorem ipsum dolor sit amet,consectetur .adipisicing elit,sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua.Ut enim ad minim veniam,
quis nostrud exercitation ullamco 在工作人员中,女士们,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性,女性 n code> pre>
div>
答
simply do this ..
if(strlen($body) > 150) {
$body = substr($body , 0 , 150);
}
echo $body;
or simply remove the if statement and use
$body = substr($body , 0 , 150);
答
Yeah, you should use "substr" (http://www.php.net/manual/de/function.substr.php)
echo substr('abcdef', 1, 3);
$title = "Post Title";
$body = "Lorem ipsum dolor sit amet, consectetur .adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum";
$newVar = substr($body, 0, 150);