Vivid Lust Posted February 19, 2009 Share Posted February 19, 2009 Hey all! How in PHP could I limit the number of characters which a string could contain? Such as the following string: This ?is a string Is limited to 10 characters: This ?is a Can someone help? Thanks. Link to comment https://forums.phpfreaks.com/topic/145903-solved-limit-number-of-characters-in-a-string/ Share on other sites More sharing options...
Mchl Posted February 19, 2009 Share Posted February 19, 2009 use substr for example Link to comment https://forums.phpfreaks.com/topic/145903-solved-limit-number-of-characters-in-a-string/#findComment-766023 Share on other sites More sharing options...
Vivid Lust Posted February 19, 2009 Author Share Posted February 19, 2009 Would it be possible for someone to write me the code? Thanks Link to comment https://forums.phpfreaks.com/topic/145903-solved-limit-number-of-characters-in-a-string/#findComment-766024 Share on other sites More sharing options...
runnerjp Posted February 19, 2009 Share Posted February 19, 2009 <?php $words = This ?is a string; echo substr ($words, 0, 10); ?> Link to comment https://forums.phpfreaks.com/topic/145903-solved-limit-number-of-characters-in-a-string/#findComment-766030 Share on other sites More sharing options...
runnerjp Posted February 19, 2009 Share Posted February 19, 2009 sorry forgot '' <?php $words = 'This ?is a string'; echo substr ($words, 0, 10); ?> Link to comment https://forums.phpfreaks.com/topic/145903-solved-limit-number-of-characters-in-a-string/#findComment-766031 Share on other sites More sharing options...
Vivid Lust Posted February 19, 2009 Author Share Posted February 19, 2009 Thanks! How could I then check to see if I needed to cut anything off?? (last question). Thanks. Link to comment https://forums.phpfreaks.com/topic/145903-solved-limit-number-of-characters-in-a-string/#findComment-766035 Share on other sites More sharing options...
runnerjp Posted February 19, 2009 Share Posted February 19, 2009 Thanks! How could I then check to see if I needed to cut anything off?? (last question). Thanks. dont understand? Link to comment https://forums.phpfreaks.com/topic/145903-solved-limit-number-of-characters-in-a-string/#findComment-766037 Share on other sites More sharing options...
Vivid Lust Posted February 19, 2009 Author Share Posted February 19, 2009 If it the string was: hello Then no characters would be "cut off". I would like to see if any characters have been "cut off", as to know if to include a "..." Hope this is more clear. Link to comment https://forums.phpfreaks.com/topic/145903-solved-limit-number-of-characters-in-a-string/#findComment-766040 Share on other sites More sharing options...
Ayon Posted February 19, 2009 Share Posted February 19, 2009 something like this? <?php $string = 'This is the text to check'; if (strlen($string) > 10) { $string = substr($string, 0, 10); } echo $string; } ?> google is a great source... just add "php" first and the question.. I.E: php check length of string Result i got: http://www.google.no/search?hl=no&q=php+check+length+of+string&meta=&aq=f&oq= Link to comment https://forums.phpfreaks.com/topic/145903-solved-limit-number-of-characters-in-a-string/#findComment-766042 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.