Click to See Complete Forum and Search --> : strstr


lukezweb
02-08-2004, 08:53 AM
how do you remove the last two letters of a string?

i know u can get them by using strstr() with -2 but how do you remove them say from this....


$test="test";
$test2=remove last two letters;
echo "$test2";

which then gives 'te'

piersk
02-08-2004, 08:58 AM
$test2 = substr($test,0-2);

I think...

pyro
02-08-2004, 09:55 AM
You forgot a comma in there, Piers. ;)

$test2 = substr($test, 0, -2);

piersk
02-08-2004, 09:57 AM
Picky picky pick ;)

pyro
02-08-2004, 10:04 AM
:p

lukezweb
02-08-2004, 01:34 PM
Originally posted by pyro
:p


ah thanks alto but , i forgot to say im using......


<?php
while ($row = mysql_fetch_array($users))
{
echo "".$row['username'].", ";
}
?>


hence how do i remove the last comma from the last user brought out
echo "".$row['username'].", ";

thanks in advance :)

pukka
02-08-2004, 02:19 PM
<?php
while ($row = mysql_fetch_array($users))
{
$foo = substr($row['username'], 0, -2);
echo "".$foo.", ";
}
?>

not sure if this is what you wanted. i didn't really understand what you meant.

lukezweb
02-08-2004, 02:45 PM
Originally posted by pukka
<?php
while ($row = mysql_fetch_array($users))
{
$foo = substr($row['username'], 0, -2);
echo "".$foo.", ";
}
?>

not sure if this is what you wanted. i didn't really understand what you meant.

you removed the last two from username list, i want to do what ive doen but maybe instead of echo i add strstr in so after putting in all the username (this is an sql statement) it removes the last , as it adds a , and a space after each right now :)

pukka
02-09-2004, 02:56 PM
you've completely confused me

http://www.php.net/manual/en/function.strstr.php