Page 1 of 1

text manipulation...

Posted: Mon Jan 19, 2004 12:14 pm
by lizlazloz
hi, i'm pretty new to php, what i want to do is something like this....

i have a variable, called $names, whcih is a list of names, like this:

bob,tom,jim,sam,rich,mike

what i want is for php to extract the top name from the list, echo that (then add a <br> or something), get rid of the comma, echo the next name, get rid of the comma, etc etc....

please help :cry:

Posted: Mon Jan 19, 2004 12:16 pm
by malcolmboston
make a loop

Posted: Mon Jan 19, 2004 12:28 pm
by lizlazloz
i know that.... but how do 'cut out' the name, and remove the comma?

Posted: Mon Jan 19, 2004 12:28 pm
by dull1554
why don't you use an array,
then create a while loop that does what you want
like this

Code: Select all

<?php
$array = array
(
0 => 'ryan',
1 => 'luke',
2 => 'sally',
3 => 'john'
);
$int = "0";
$max = "4";
while($int < $max)
{
    echo $array[$int]."<br>";
    $int++;
}
?>

Posted: Mon Jan 19, 2004 12:31 pm
by malcolmboston
loops, operators etc are all covered in the docs

Posted: Mon Jan 19, 2004 12:35 pm
by dull1554
i know that, but you asked, i simply gave a you a soultion.

do the names have to be in one $var, if so you could explode() it at the ","

Posted: Mon Jan 19, 2004 6:25 pm
by pickle
ya, explosion is the best.

$name_array = explode(',',$names);
for($counter = 0,$curr_name = $name_array[$counter],$counter++)
{
echo "$curr_name <br />";
}

That should do it.

Posted: Mon Jan 19, 2004 6:31 pm
by markl999
Or :
echo join('<br />', explode(',', $names));
..or..
echo str_replace(',', '<br />', $names);