JJohnsenDK Posted February 18, 2007 Share Posted February 18, 2007 Hey I want to cut down a string like this: <?php $text = "Hey you, you funny dude"; $text2 = strlen($text); if($text2 > 15){ echo "Hey you, yo...."; } ?> what function can i use to cut in the string like in the example: If $text2 varible is over 15 show the first 15 letters and after that put in four .... hope someone can help Link to comment https://forums.phpfreaks.com/topic/39020-solved-function-to-cut-strings/ Share on other sites More sharing options...
ShogunWarrior Posted February 18, 2007 Share Posted February 18, 2007 $text2 = ( ( strlen($text)>15 )?( substr($text,0,15) ) $text ) ); Short and sweet. Link to comment https://forums.phpfreaks.com/topic/39020-solved-function-to-cut-strings/#findComment-187883 Share on other sites More sharing options...
Orio Posted February 18, 2007 Share Posted February 18, 2007 And if you want to add the three dots (based on ShogunWarrior's code): $text2 = (strlen($text) > 15) ? substr($text, 0, 15)."..." : $text; Orio. Link to comment https://forums.phpfreaks.com/topic/39020-solved-function-to-cut-strings/#findComment-187889 Share on other sites More sharing options...
JJohnsenDK Posted February 18, 2007 Author Share Posted February 18, 2007 that is short and sweet, i most say ... but could you explain to me what the quesstionmark does? I never figured that out... Link to comment https://forums.phpfreaks.com/topic/39020-solved-function-to-cut-strings/#findComment-187890 Share on other sites More sharing options...
trq Posted February 18, 2007 Share Posted February 18, 2007 Take a look at the ternary operator on this page. It basically means.... <?php "if this is true" ? "do this" : "else, do this" ?> Link to comment https://forums.phpfreaks.com/topic/39020-solved-function-to-cut-strings/#findComment-187891 Share on other sites More sharing options...
JJohnsenDK Posted February 18, 2007 Author Share Posted February 18, 2007 aaah alot.. thanks to all for you Link to comment https://forums.phpfreaks.com/topic/39020-solved-function-to-cut-strings/#findComment-187894 Share on other sites More sharing options...
redarrow Posted February 18, 2007 Share Posted February 18, 2007 The easy way i say. <?php $word="my name is reddarrow i like php"; $result=wordwrap($word,21,"<br>",1); echo $result; ?> Link to comment https://forums.phpfreaks.com/topic/39020-solved-function-to-cut-strings/#findComment-187904 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.