Hi,
I was wondering how you would cut a string in PHP to get the first 50 letters of it? Say I have a 200 word variable and I want to get the first 50 letters to the left of it. How would I go about doing so?
Regards,
Matt
SOLVED - How do you cut a string?
Moderator: General Moderators
SOLVED - How do you cut a string?
Last edited by matth2004 on Sat Feb 17, 2007 6:53 am, edited 1 time in total.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
There's probably a better solution, but one that comes to mind is:
However, I can see that cutting off the last word of your string completely.
You could also make a loop using strpos() and concatenating each character to a new string until you've hit 50 characters.
Code: Select all
$newstr = wordwrap($string, 50, '\n', true);
$newstr = explode('\n', $newstr);
$string = $newstr[0];You could also make a loop using strpos() and concatenating each character to a new string until you've hit 50 characters.
I just found the substr function where you do:
Thanks,
Matt
Code: Select all
$cutstring = substr($var, 0, 50);
Matt
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm