refiking Posted July 1, 2009 Share Posted July 1, 2009 I think I got an effective way to count the number of words in a string, but that's as far as I got. I then need to delete all the other words that preceed the 10th word. Here's what I have so far... IF (count(explode(" ",$row['title'])) > 10){ //delete the other words $newtitle = //code to delete the other words //update mysql } Quote Link to comment https://forums.phpfreaks.com/topic/164426-solved-delete-all-words-after-10th-word-in-a-string/ Share on other sites More sharing options...
mattal999 Posted July 1, 2009 Share Posted July 1, 2009 <?php $title = explode(" ", $row['title']); foreach($title as $word) { $count = $count + 1; if($count =< 10) { $newtitle = $newtitle." ".$word; } } ?> Try that. Quote Link to comment https://forums.phpfreaks.com/topic/164426-solved-delete-all-words-after-10th-word-in-a-string/#findComment-867327 Share on other sites More sharing options...
DavidAM Posted July 1, 2009 Share Posted July 1, 2009 explode it into an array and then take a slice of the array $words = explode(" ", $row['title']); if (count($words) > 10) { $words = array_slice($words, 0, 10); // First Ten words $row['title'] = implode(" ", $words); } Quote Link to comment https://forums.phpfreaks.com/topic/164426-solved-delete-all-words-after-10th-word-in-a-string/#findComment-867337 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.