LiamProductions Posted September 19, 2007 Share Posted September 19, 2007 Hey. Im kind of stuck. Well... If im getting like a subject title from a database. How would i get the first 10 chars and then have like 3 dots after it? What functions should i be using or could someone post an example. Cheers, Liam. Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/ Share on other sites More sharing options...
Wuhtzu Posted September 19, 2007 Share Posted September 19, 2007 <?php $title_from_db = 'Something longer than 10 chars'; $short_title = substr($title_from_db, 0, 10) . '...'; ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/#findComment-351208 Share on other sites More sharing options...
BlueSkyIS Posted September 19, 2007 Share Posted September 19, 2007 ... and/or use the MySQL SUBSTR() and CONCAT() functions... Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/#findComment-351209 Share on other sites More sharing options...
remlabm Posted September 19, 2007 Share Posted September 19, 2007 try something like this: $preview = substr($subject, 0, 10)."..."; http://us3.php.net/manual/en/function.substr.php Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/#findComment-351210 Share on other sites More sharing options...
redarrow Posted September 19, 2007 Share Posted September 19, 2007 caternation way. <?php $var="my-name-is-redarrow"; $a=substr($var,0,10); $b="..."; echo $a.$b; ?> Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/#findComment-351215 Share on other sites More sharing options...
BlueSkyIS Posted September 19, 2007 Share Posted September 19, 2007 let's all say the same thing 3 different times Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/#findComment-351217 Share on other sites More sharing options...
redarrow Posted September 19, 2007 Share Posted September 19, 2007 i worked it out there you go....... <?php $var="my name is redarrow iamthemansowhat!"; $a=explode(' ',$var); foreach($a as $b){ if(strlen($b)>=10){ $c=substr($b,0,10)."...."; $d=str_replace("$b","$c",$var); echo $d; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/#findComment-351225 Share on other sites More sharing options...
LiamProductions Posted September 19, 2007 Author Share Posted September 19, 2007 thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/#findComment-351230 Share on other sites More sharing options...
Wuhtzu Posted September 19, 2007 Share Posted September 19, 2007 redarrow, please confirm that your last reply was irony / sarcasm. Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/#findComment-351282 Share on other sites More sharing options...
tibberous Posted September 19, 2007 Share Posted September 19, 2007 Except if the title is shorter your guys code will still add the dots. (strlen($_ = substr($title_from_db, 0, 10)) > 12 ? "$_..." : $title_from_db); Written but not tested, might have messed up the () or off by one'd it or something - that is the idea. Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/#findComment-351302 Share on other sites More sharing options...
Wuhtzu Posted September 19, 2007 Share Posted September 19, 2007 He asked for ideas, not a complete solution. 469 posts and unable to construct an if-statement does not add up But you are right, he might want to make sure the title is actually longer than 10 chars before appending the dots. Quote Link to comment https://forums.phpfreaks.com/topic/69928-any-ideas-on-this/#findComment-351312 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.