cgm225 Posted August 7, 2007 Share Posted August 7, 2007 If I have a long string of text in a variable, how can I "trim" the text down to a specific number of characters? So for example, if I have: $string = "This is a string"; and I just want the first four characters of the string, so just "This", how would I do that? Thank you in advance! cgm225 Quote Link to comment https://forums.phpfreaks.com/topic/63645-solved-limiting-number-of-characters-in-a-string/ Share on other sites More sharing options...
teng84 Posted August 7, 2007 Share Posted August 7, 2007 echo substr("Hello world!",6,5); Quote Link to comment https://forums.phpfreaks.com/topic/63645-solved-limiting-number-of-characters-in-a-string/#findComment-317142 Share on other sites More sharing options...
jscix Posted August 7, 2007 Share Posted August 7, 2007 substr($string, 0, 4); Quote Link to comment https://forums.phpfreaks.com/topic/63645-solved-limiting-number-of-characters-in-a-string/#findComment-317143 Share on other sites More sharing options...
Fadion Posted August 7, 2007 Share Posted August 7, 2007 $text = 'This is a string'; echo substr($text, 0, 4); substr(string, int start, int length). Quote Link to comment https://forums.phpfreaks.com/topic/63645-solved-limiting-number-of-characters-in-a-string/#findComment-317144 Share on other sites More sharing options...
cgm225 Posted August 7, 2007 Author Share Posted August 7, 2007 Thank you all! Quote Link to comment https://forums.phpfreaks.com/topic/63645-solved-limiting-number-of-characters-in-a-string/#findComment-317152 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.