text manipulation...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lizlazloz
Forum Commoner
Posts: 64
Joined: Mon Dec 29, 2003 7:29 am

text manipulation...

Post 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:
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

make a loop
lizlazloz
Forum Commoner
Posts: 64
Joined: Mon Dec 29, 2003 7:29 am

Post by lizlazloz »

i know that.... but how do 'cut out' the name, and remove the comma?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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++;
}
?>
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

loops, operators etc are all covered in the docs
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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 ","
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Or :
echo join('<br />', explode(',', $names));
..or..
echo str_replace(',', '<br />', $names);
Post Reply