jesushax Posted June 16, 2008 Share Posted June 16, 2008 hi below is my code what i want to do is not write the last work if the total characters are going to be greater that 40 <?php $title = $port["ClientCompany"].' - '.$port["ProjectTitle"]; echo substr($title,0,40); ?> Link to comment https://forums.phpfreaks.com/topic/110432-solved-substr-take-out-word-isntead-of-character/ Share on other sites More sharing options...
webent Posted June 16, 2008 Share Posted June 16, 2008 maybe something like... $titleLength=strlen($title); if ($titleLength > 40) { substr($title,0,40); // Or do whatever you want to the string if it is longer // Maybe even something like this to replace a word if it is longer... $search = array("'"); $replace = array(""); $fields = str_replace($search,$replace,$fields); } Link to comment https://forums.phpfreaks.com/topic/110432-solved-substr-take-out-word-isntead-of-character/#findComment-566594 Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 How about this? <?php $title = substr($title,0,41); $title = substr($title,0,strrpos($title,' ')); print $title; ?> Link to comment https://forums.phpfreaks.com/topic/110432-solved-substr-take-out-word-isntead-of-character/#findComment-566607 Share on other sites More sharing options...
webent Posted June 16, 2008 Share Posted June 16, 2008 Yes, LOL... rhodesa's code is much cleaner... I'm notorious for not combining built in functions... Link to comment https://forums.phpfreaks.com/topic/110432-solved-substr-take-out-word-isntead-of-character/#findComment-566610 Share on other sites More sharing options...
jesushax Posted June 16, 2008 Author Share Posted June 16, 2008 thanks works a treat Link to comment https://forums.phpfreaks.com/topic/110432-solved-substr-take-out-word-isntead-of-character/#findComment-566611 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.