Click to See Complete Forum and Search --> : parsing string in PHP


boccio
May 24th, 2002, 03:36 AM
hi all,

how can I 'cut off' string in PHP?
for example:
I have a field in dbase smth TEXT , and fill it with some text... like "This is a text, blah, blah" (of couse real text is much larger).
now, how can I create a "preview" of that so when I display result of query it shows only first few words: This is a ..... .

what i figured out is this:

$strDisplay = $row[text];
$strParse = $strDisplay{0} . $strDisplay{1} . $strDisplay{2};

which is not an option.... I'd like, say, first 20 chars to be displayed...
but how can I do like in c++ : $strParse = $strDisplay.Left(20) to take first 20 chars from left.....

TIA,
boris

Marc Rochkind
May 24th, 2002, 02:02 PM
You could use the substr function. Ideally, you would add the ... only if the length were less than the maximum.

boccio
May 24th, 2002, 03:06 PM
yes, i tried

$strDisplay = $row[text];
$strParse = substr($strDisplay,0,250) . "...";

...and it works like charm....

thx a lot