aebstract Posted October 10, 2008 Share Posted October 10, 2008 I've got some ideas on how to approach this but I just can't seem to find the exact tools I am going to need. Just some points in the right direction and some functions that I'll need would be great. Basically if my string is over 20 characters, I want to cut off everything past the 20th character and just add '..' to the end of the string and display the result. Thanks Link to comment https://forums.phpfreaks.com/topic/127858-trying-to-cut-a-string-off-and-add/ Share on other sites More sharing options...
xsist10 Posted October 10, 2008 Share Posted October 10, 2008 function str_limit($str) { return (strlen($str) > 20) ? substr($str, 0, 20) .".." : $str); } echo str_limit("this is a really really really long string"); Link to comment https://forums.phpfreaks.com/topic/127858-trying-to-cut-a-string-off-and-add/#findComment-661927 Share on other sites More sharing options...
aebstract Posted October 10, 2008 Author Share Posted October 10, 2008 function str_limit($str) { return (strlen($str) > 20) ? substr($str, 0, 20) .".." : $str); } $partname = str_limit("$partname"); Figured this should work but it's returning a white screen. Not getting anything from error reporting Link to comment https://forums.phpfreaks.com/topic/127858-trying-to-cut-a-string-off-and-add/#findComment-661944 Share on other sites More sharing options...
xsist10 Posted October 10, 2008 Share Posted October 10, 2008 You need to echo out $partname. The function just returns the shorted string. It doesn't echo it. Link to comment https://forums.phpfreaks.com/topic/127858-trying-to-cut-a-string-off-and-add/#findComment-661947 Share on other sites More sharing options...
aebstract Posted October 10, 2008 Author Share Posted October 10, 2008 That doesn't justify my entire page being a blank white, it's a syntax error or something I just am not familiar with that second line and the format it's in so I can't fix it really. Link to comment https://forums.phpfreaks.com/topic/127858-trying-to-cut-a-string-off-and-add/#findComment-661975 Share on other sites More sharing options...
xsist10 Posted October 10, 2008 Share Posted October 10, 2008 Sorry I had an out of place parenthesis. Corrected. function str_limit($str) { return (strlen($str) > 20 ? substr($str, 0, 20) .".." : $str); } $partname = str_limit($partname); Link to comment https://forums.phpfreaks.com/topic/127858-trying-to-cut-a-string-off-and-add/#findComment-661980 Share on other sites More sharing options...
aebstract Posted October 10, 2008 Author Share Posted October 10, 2008 Hmm still white paging Link to comment https://forums.phpfreaks.com/topic/127858-trying-to-cut-a-string-off-and-add/#findComment-661984 Share on other sites More sharing options...
wildteen88 Posted October 10, 2008 Share Posted October 10, 2008 Why dont you enable display_errors within your php.ini that way you wont get a blank page when an error occurs. Link to comment https://forums.phpfreaks.com/topic/127858-trying-to-cut-a-string-off-and-add/#findComment-662106 Share on other sites More sharing options...
nickthegreek Posted October 21, 2008 Share Posted October 21, 2008 $string = "1 This is a test 2 this is a test 3 this is a test 4 this is a test"; $string2 = substr($string,0,20); echo $string2; if (strlen($string)>20){echo "...";} //result = "1 This is a test 2 t..." Link to comment https://forums.phpfreaks.com/topic/127858-trying-to-cut-a-string-off-and-add/#findComment-670785 Share on other sites More sharing options...
ghostdog74 Posted October 21, 2008 Share Posted October 21, 2008 I've got some ideas on how to approach this but I just can't seem to find the exact tools I am going to need. Just some points in the right direction and some functions that I'll need would be great. Basically if my string is over 20 characters, I want to cut off everything past the 20th character and just add '..' to the end of the string and display the result. Thanks echo substr($string,0,20) . ".."; Link to comment https://forums.phpfreaks.com/topic/127858-trying-to-cut-a-string-off-and-add/#findComment-670879 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.