0

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";
Sharjeel
  • 76
  • 7
  • 1
    Questions asking for code must **demonstrate a minimal understanding of the problem being solved**. Include attempted solutions, why they didn't work, and the expected results. See also: [Stack Overflow question checklist](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist). – John Conde Apr 16 '14 at 14:27
  • possible duplicate of [How can I truncate a string to the first 20 words in PHP?](http://stackoverflow.com/questions/965235/how-can-i-truncate-a-string-to-the-first-20-words-in-php) – Amal Murali Apr 16 '14 at 14:27
  • 1
    _i am new in this field so excuse me plz_ – Sharjeel Apr 16 '14 at 14:32

2 Answers2

3

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);
Azeem Hassni
  • 885
  • 13
  • 28
1

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);
Tyralcori
  • 1,079
  • 13
  • 33