etrader Posted January 24, 2011 Share Posted January 24, 2011 I have a string in the form of "word1-word2-word3-word4-word5-word-6-word7", but I want to shorten it to 5 words only. Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/ Share on other sites More sharing options...
Vander Posted January 24, 2011 Share Posted January 24, 2011 This might be a crude way of doing it, but I'll suggest it anyways. <?php $string = 'word1-word2-word3-word4-word5-word-6-word7'; $count = explode('-',$string); $newstring = ''; for($x=0;$x<5;$x++): $newstring .= '-'.$count[$x]; endfor; $newstring = substr($newstring,1); echo "$string<br />$newstring"; ?> Edit: I messed up first time around with the code. Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164502 Share on other sites More sharing options...
Maq Posted January 24, 2011 Share Posted January 24, 2011 You can also use regular expressions. This way would also be a bit more dynamic if you ever want to change requirements: $subject = "word1-word2-word3-word4-word5-word6-word7"; $pattern = "/(\w*-){5}/i"; preg_match($pattern, $subject, $matches); echo $matches[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164505 Share on other sites More sharing options...
etrader Posted January 24, 2011 Author Share Posted January 24, 2011 <?php $string = 'word1-word2-word3-word4-word5-word-6-word7'; $count = explode('-',$string); $newstring = ''; for($x=0;$x<5;$x++): $newstring .= '-'.$count[$x]; endfor; $newstring = substr($newstring,1); echo "$string<br />$newstring"; ?> When the number of words is lesser than the limit defined, it adds "-"s at the end of the string. Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164513 Share on other sites More sharing options...
etrader Posted January 24, 2011 Author Share Posted January 24, 2011 <?php $subject = "word1-word2-word3-word4-word5-word6-word7"; $pattern = "/(\w*-){5}/i"; preg_match($pattern, $subject, $matches); echo $matches[0]; ?> As I tried, this returns nothing. Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164519 Share on other sites More sharing options...
Vander Posted January 24, 2011 Share Posted January 24, 2011 When the number of words is lesser than the limit defined, it adds "-"s at the end of the string. As I said, it was crude... But surely you can work with it? Or are you expecting other people to do most of the work for you? Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164522 Share on other sites More sharing options...
etrader Posted January 24, 2011 Author Share Posted January 24, 2011 As I said, it was crude... But surely you can work with it? Or are you expecting other people to do most of the work for you? No, I can replace "-"s by means of str_replace; but I am not sure if it is the simplest way to do so. Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164528 Share on other sites More sharing options...
Maq Posted January 24, 2011 Share Posted January 24, 2011 $subject = "word1-word2-word3-word4-word5-word6-word7"; $pattern = "/(\w*-){5}/i"; preg_match($pattern, $subject, $matches); echo $matches[0]; ?> As I tried, this returns nothing. Works fine for me. Did you implement this with other code? Also, to get rid of the last dash, change the regex pattern to: $pattern = "/(\w*-){4}\w*/i"; Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164532 Share on other sites More sharing options...
etrader Posted January 24, 2011 Author Share Posted January 24, 2011 This works well, only when the number of words are higher than the limit. If the number of words are originally lower than the limit, it returns nothing. Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164540 Share on other sites More sharing options...
Maq Posted January 24, 2011 Share Posted January 24, 2011 If it doesn't match print out the original text or whatever you please: $subject = "word1-word2-word3"; $pattern = "/(\w*-){4}\w*/i"; if(preg_match($pattern, $subject, $matches)) { echo $matches[0]; } else { echo $subject; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164543 Share on other sites More sharing options...
ignace Posted January 24, 2011 Share Posted January 24, 2011 $str = implode('-', array_slice(explode('-', $str), 0, 5)); Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164562 Share on other sites More sharing options...
Maq Posted January 24, 2011 Share Posted January 24, 2011 There's always an elegant string function solution. Quote Link to comment https://forums.phpfreaks.com/topic/225518-limiting-the-string-length-by-the-number-of-words/#findComment-1164594 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.