Page 1 of 1

SOLVED - How do you cut a string?

Posted: Sat Feb 17, 2007 6:31 am
by matth2004
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

Posted: Sat Feb 17, 2007 6:49 am
by superdezign
There's probably a better solution, but one that comes to mind is:

Code: Select all

$newstr = wordwrap($string, 50, '\n', true);
$newstr = explode('\n', $newstr);
$string = $newstr[0];
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.

Posted: Sat Feb 17, 2007 6:53 am
by matth2004
I just found the substr function where you do:

Code: Select all

$cutstring = substr($var, 0, 50);
Thanks,
Matt

Posted: Sat Feb 17, 2007 6:58 am
by superdezign
That's right. The simple solutions always slip my mind.